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.
you can use this function in input field
(keypress)="onChange($event.keyCode)"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With