'Key code from KeyboardEvent in android is unidentified?

I will get a Char from keydown event.

In all browser i can use event.key and its work fine, but in android result is something else

event.key: unidentified
event.code: ''
event.which: `229` (for [a-z0-9] is always this number)
window.event.keyCode: `229`

hire is old stackoverflow post, but it dos't work any more.

codepen demo for test in android (IOS and PC work fine)

How can i get key code or key string from KeyboardEvent



Solution 1:[1]

Although this question is pretty old, I got here while searching for a similar thing and maybe this can help others.

Assuming you get text in HTML via:

<textarea id="my_input"></textarea>

or:

<input id="my_input"></input>

And assuming you want to get the last char entered in JavaScript, it works if you get the value of the input and slice everything away except the last char:

document.addEventListener('keyup', function(event) {
    val lastchar = document.getElementById('my_input').value.slice(-1);
});

Note: lastchar will also hold special characters like new line.

This works on Android 12 while (as you pointed out) event.key does not.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fr87