6

I am trying to alert the key which was pressed. It is working on PC but returning "Unidentified" on mobile. Why is that?

$(document).on("keydown", function(e) {
     alert(e.key);
   });
jokoka7524
  • 83
  • 1
  • 6
  • Does this answer your question? [Capture keys typed on android virtual keyboard using javascript](https://stackoverflow.com/questions/30743490/capture-keys-typed-on-android-virtual-keyboard-using-javascript) – sfarbota Jan 03 '20 at 19:21
  • @sfarbota no, i am not working with input fields, i want to capture all the keystrokes, not just on specified input fields – jokoka7524 Jan 03 '20 at 19:23
  • You should be using the `input` event or touch events. There are no keys on a mobile device. – Scott Marcus Jan 03 '20 at 19:40
  • @Scott Marcus yes, but the virtual keyboard seems to have keycodes which can be converted to unicode characters using `.key`. – jokoka7524 Jan 03 '20 at 19:47
  • 2
    I ran to similar problem on scan code readers: keys were marked as "unidentified" due to wrong system settings which mapped the scanning to virtual keyboard. It was on Android 10 and Chrome and when properly set, it worked as expected. – Jan Turoň Aug 12 '21 at 12:07

2 Answers2

7

This is a known bug in Chrome on Android. See this other question and the official bug report. I wish I had better news for you, but it looks like you can't rely on keyboard triggers in web apps on Android if you need to capture the individual key values.

sfarbota
  • 2,619
  • 1
  • 22
  • 30
0

What you need to do is catch the e.target.value:

$(document).on("keydown", function(e) {
  var inputValue = e.target.value;
  var lastLetter = inputValue.charAt(inputValue.length - 1);
  alert('Letter: ' + lastLetter);
});
Shlomtzion
  • 674
  • 5
  • 12