I'm trying to get the value of the keyPressed in either a keyPress
, keydown
, or keyUp
event.
The different solutions here works well on Chrome, FireFox, IE etc. However they fail on android devices.
I tried several solutions inspired from the following SO:
- Get Character value from KeyCode in JavaScript... then trim
- How to convert keycode to character using javascript
- how to get String/character value from key Code;
First Solution:
element.keydown(function (e) {
var keyCode = e.charCode || e.keyCode || e.which;
var digit = String.fromCharCode(keyCode);
// here digit is "å" on android using danish locale
}
KeyUp yields same result. KeyPressed will not fire on chrome on android.
To reproduce i made a plunkr, which shows the behaviour:
https://plnkr.co/BVUfiJrBr0RBPDst9izD/
It works fine on desktop but the keyCode is always 229
on android browsers, regardless of the key pressed.
I reproduced the error on:
- Samsung Galaxy S6, Android 5.1.1, Browser: Chrome 48.0.2564.95
- Sony Experia Z2, Android 5.1.1, Browser: Chrome 48.0.2564.95
It did work on:
- Samsung Galaxy S6, Android 5.1.1, Browser: Firefox
- Iphone
Trying to reproduce the error indicates a bug in Chrome. Is there a different way around this issue?