使用javascript在Android虚拟键盘上键入捕获键 [英] Capture keys typed on android virtual keyboard using javascript

查看:256
本文介绍了使用javascript在Android虚拟键盘上键入捕获键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有textarea的网页,我需要捕获用户键入的键(这样我就可以用键入的键替换不同的unicode字符)。我目前的代码如下:

I have a web page with a textarea, and I need to capture the keys typed by the user (so that I can substitute different unicode characters for the keys typed). My current code is as follows:

$("#myTextArea").bind('keypress', function(event) {

    var keyInput = event.which;
   // call other functions

});

以上代码适用于PC和iPhone / Safari。但是,在Android (三星)平板电脑上使用Chrome时,会失败。出于某种原因,当我在 android虚拟(软)键盘上键入时,按键事件不会被触发。 Android版本是5.0.2。

This above code works on PCs, and iPhone/Safari. However, it fails when using Chrome on an android (samsung) tablet. For some reason when I type on the android virtual (soft) keyboard, the "keypress" event is not triggered. The android version is 5.0.2.

如果我尝试使用keyUp或keyDown,它总是为所有字符返回229 (除了返回键,空格,退格键等)。

If I try using "keyUp" or "keyDown", it always returns 229 for all characters (except for return key, space, backspace, etc).

即使keyCode始终为229,textarea也会显示用户键入的正确字符。这意味着设备知道输入了哪个密钥,但不知怎的,我无法使用javascript来处理此事件(和密钥代码)。

Even though the keyCode is always 229, the textarea displays the correct characters typed by the user. Which means the device knows which key was entered, but somehow I'm unable to get a handle on this event (and the key code) using javascript.

这是到目前为止我尝试过的替代方案及其结果:

Here are the alternatives that I have tried so far, and their outcomes:

$("#mainTextArea").on("keydown keyup", function(event) { 
    // event.which and event.keyCode both return 229

$(document).on('keypress', function(event) { 
    // function is not triggered

$('#myTextArea').bind('input keypress', function(event) { 
   // comes inside function, but keyCode and which are undefined

有关此问题的任何帮助表示赞赏..

Any help regarding this issue is appreciated..

推荐答案

不幸的是,你似乎在这里做不了多少。
Keypress事件已被弃用,因此没有被解雇。
229 on keyup和keydown表示键盘缓冲区忙原因 - 当你按一个键时 - 输入仍然不能保证用户按下了什么,因为自动建议和其他事件可能会立即跟随并使事件无效。
虽然在我看来最先发送密钥会更好,然后在自动建议中触发另一个事件,这样你就可以单独行动......

Unfortunately it seems you cannot do much here. Keypress event is deprecated, thus not fired. 229 on keyup and keydown indicates the keyboard buffer is busy. The reason - when you press a key - the input is still not guaranteed to be what the user pressed, because of auto suggest and other events that may follow immediately and invalidate the event. Although in my opinion it would have been better to send the key first, then fire another event perhaps on auto suggest so you can act upon it separately...

我目前唯一知道的是连接到keydown和keyup,将值存储在keydown上,获取keyup上的值并找到delta,这是用户按下的。不幸的是,这对于非输入控件(例如 - 身体或类似的东西)不起作用。
也许不是你想听到的答案,但仍然。

The only thing that I currently know of is to attach to both - keydown and keyup, store the value on keydown, get the value on keyup and find the delta, which is what user pressed. Unfortunately this will not work for non-input controls (e.g. - the body or something like that). Maybe not what you want to hear as answer but still.

这篇关于使用javascript在Android虚拟键盘上键入捕获键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆