Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 ionic 2 handling events on keypress

I have a bunch of ionic 2 cards which I want to flip on the press of a key (any key, it doesn't matter). The code looks like

<ion-content padding>
    <ion-card (click)="setTime(7)" *ngIf="status == 'morning'" (keypress)="eventHandler($event)" style="width:80%">
        <img src="https://greatist.com/sites/default/files/Sleeping-Positions-feature.jpg"/>
    </ion-card>
</ion-content>

the .ts code

eventHandler(keyCode){
        alert('hey vikj');
  }

On pressing any key, my event handler is not fired.

like image 874
Vik Avatar asked Dec 01 '25 05:12

Vik


2 Answers

you can use this function in input field

(keypress)="onChange($event.keyCode)" 
like image 70
Atif Hussain Avatar answered Dec 02 '25 20:12

Atif Hussain


It's set up correct, but the focus needs to be on the ion-card before it starts to listen. Click on the card and then press a key and it should work. If you want the focus to be on the entire page check out this question: Angular 2 | listen for keypress event on whole page

like image 33
Z. Bagley Avatar answered Dec 02 '25 21:12

Z. Bagley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!