Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - Keydown Space on Mobile

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.

like image 241
Flo Avatar asked Mar 05 '26 22:03

Flo


1 Answers

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) ...
}
like image 140
Mark Avatar answered Mar 08 '26 10:03

Mark



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!