miladfm
miladfm

Reputation: 1526

Key code from KeyboardEvent in android is unidentified?

I will get a Char from keydown event.

In all browsers I can use event.key and it works fine, but in android the result is something else:

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

Here is an old stackoverflow post, but it doesn'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

Upvotes: 11

Views: 3649

Answers (2)

TamoMbobda
TamoMbobda

Reputation: 11

i was going trough the same problem in android. i found out that using the input event solved the problem. this event contains the whole string which is actualy typed so if you just one the last character typed just take the last character of the string "event.data"

        document.addEventListener('input',function(event){
        console.log(event)
        let word = event.data === null ? '' : event.data
        input.search(word)

    })

Upvotes: 1

fr87
fr87

Reputation: 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.

Upvotes: 0

Related Questions