3

I have an input where I listen for the keydown.space event. This technique works fine on the desktop, but the event isn't triggered on mobile. My HTML template looks like the following:

<input
    type="text"
    (keydown.space)="onAdd()"
    (keydown.backspace)="onRemoveLast()"
>

So I need a way to listen to the space key event on the software keyboard of mobile phones.

Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137

2 Answers2

2

What "mobile" did you test it on? If it's Android, like William Moore said, it may be an issue of Android not registering spacebar keypress as the standard 32.

Otherwise, you can try onkeypress + listening for the keycode on the JS side like so:

<input
  type="text"
  onkeypress="onAdd(event)"
  (keydown.backspace)="onRemoveLast()"
>

function onAdd(event){
  if (event.keyCode === 32) ...
}
Mark
  • 139
  • 7
0

That's what I did to make it work:

HTML

<input
    [(ngModel)]="input"
    (keydown.backspace)="onRemoveLast()"
    (ngModelChange)="onChange()"
>

Typescript

public onChange(): void {
    if (this.input.substr(-1) === ' ') this.add();
}
Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137