keyCode on android is always 229

2019-01-14 20:21发布

On my Samsung Galaxy tab 4 (Android 4.4.2, Chrome: 49.0.2623.105) I ran into a situation where the keyCode is always 229.

I've setup a simple test for two situation

<div contenteditable="true"></div>
<input>
<span id="keycode"></span>

script:

$('div, input').on('keydown', function (e) {
    $('#keycode').html(e.keyCode);
});

DEMO

Fortunately I can find posts about this, but I couldn't find one with a working solution. Someone suggested to use keyup instead or to use the textInput event, but that one is only fired on blur.

Now, to top it all, this doesn't happen with the default stock browser :(

Any help would be appreciated!

UPDATE: If it turns out that this is not possible I can still grab the char before the caret: post

6条回答
成全新的幸福
2楼-- · 2019-01-14 21:09

I was having the same issue on Samsung S7 phones. Solved it by replacing event from keydown to keypress.

$("div, input").keypress(function (e) {
    $("#keycode").html(e.which); 
});

jQuery normalizes this stuff, so there's no need to use anything other than e.which https://stackoverflow.com/a/302161/259881

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-14 21:09


I know I'm answering an old post, but yet this problem is still ON so I like to share a view on it.
However this method is not an exact solution, but just a solution for urgency as I had.
The key is to use the value of the textbox while using keypress. for every key press value will change and by the last value update we can make which key has been pressed.
Note: this only works for the visible font on the keyboard, i.e., alphanumerics and special chars, this will not record any control or shift or alt keys as you know

  $('input').keyup(function () {
  var keyChar = $(this).val().substr(-1);

 //you can do anything if you are looking to do something with the visible font characters
  });
查看更多
混吃等死
4楼-- · 2019-01-14 21:13

Running into the same problem, only happens with stock Samsung keyboard on Android. A work around was to turn off the keyboard predictions, which fixed the input. Still analysing further to see if a work around can be found in JS land.

Edit: I've managed to find a solution for our case. What was happening, is that we had a whitelist of allowed characters that a user was allowed to enter in our input box. These were alphanumeric characters plus some whitelisted control characters (e.g. enter, esc, up/down). Any other character input would have the event default prevented.

What happened is that all events with keycode 229 were being prevented, and as a result no text was entered. Once we added keycode 229 to the whitelist as well, everything went back to functioning ok.

So if you are using some kind of custom or 3rd party form input control component, make sure to check that keycode 229 is whitelisted/allowed and not default prevented.

Hope this helps someone.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-14 21:23

Try e.which instead of e.keyCode

For more information on this property check https://api.jquery.com/event.which/

查看更多
戒情不戒烟
6楼-- · 2019-01-14 21:26

I had the same issue and could not find any solutions. I solved it using javascript.

event.target.value.charAt(event.target.selectionStart - 1).charCodeAt()

查看更多
Emotional °昔
7楼-- · 2019-01-14 21:27

Normal keypress event does not give keyCode in android device. There has already been a big discussion on this.

If you want to capture the press of space bar or special chars, you can use textInput event.

$('input').on('textInput', e => {
     var keyCode = e.originalEvent.data.charCodeAt(0);
     // keyCode is ASCII of character entered.
})

Note: textInput does not get triggered on alphabets, number, backspace, enter and few other keys.

查看更多
登录 后发表回答