10

According to this link https://ionicframework.com/docs/api/input#events ionic supports keypress event which is working fine in the browser but in mobile device it is not triggering

html

<ion-item>
  <ion-label>Default Label</ion-label>
  <ion-input (keypress)="test($event)"></ion-input>
</ion-item>

ts

test(event){

console.log(event);
 e.preventDefault();
}

the above code works fine in ionic serve not in mobile device

According to this https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event keypress is depricated i am not understanding

Events i tried: keypress, keydown, keyup, ionblur, ionChange, input,..etc Either it is not working or it or preventdefault is not working

Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117
  • Facing the same issue. neither (ionInput) nor (keypress) works. Did you find a way in order for it to work? – mluis Aug 12 '19 at 09:49

3 Answers3

5

According to your own first link I guess you are looking for the event ionInput Emitted when a keyboard input ocurred. :

<ion-item>
  <ion-label>Default Label</ion-label>
  <ion-input (ionInput)="test($event)"></ion-input>
</ion-item>
Gilsido
  • 542
  • 1
  • 3
  • 11
  • no i dont want any value to be displayed as you can see i have given `event.preventDefault()` – Mohan Gopi Jul 08 '19 at 10:35
  • And `event.preventDefault()` doesn't work with `ionInput`? If it doesn't I'd try catching the keyboard events with a `@HostListner()` but it may not be the cleanest way – Gilsido Jul 08 '19 at 11:14
2

Pass $event to your event handler. The $event is a DOM KeyboardEvent.

<input type=text (keypress)="eventHandler($event)">

eventHandler(event) {
   console.log(event, event.keyCode, event.keyIdentifier);
} 

If you know which KeyboardEvent property you want, you can pass that into your event handler:

<input type=text (keypress)="eventHandler($event.keyCode)">

eventHandler(keyCode) {...}
Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
0

Use readonly attribute with ionInput event from @Gilsidoo answer, or other event as keyPress which might work in ur case.

<ion-item>
  <ion-label>Default Label</ion-label>
  <ion-input readonly (ionInput)="test($event)"></ion-input>
</ion-item>

EDIT: I suppose onInput with readonly will not work, try using keyup may be.

3960278
  • 756
  • 4
  • 12