Android WebView 中的键盘长按键 [英] Keyboard Long Key Press in Android WebVIew

查看:37
本文介绍了Android WebView 中的键盘长按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何在 Android WebView 中处理长按键盘键.我以为我只能使用 KeyboardEvent repeat 属性(https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat)但是当我使用以下代码对其进行测试时,尽管按住了键,但返回的重复属性始终为 false.

Anyone have any ideas how to handle a long press of a keyboard key in Android WebView. I had assumed I would simply be able to use the KeyboardEvent repeat property (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat) but when I tested it with the following code, the repeat property returned is always false, despite holding a key down.

document.addEventListener('keydown', logKey);

function logKey(event) {
    console.log(event.repeat);
}

以上代码在桌面浏览器上按预期工作,并在按住某个键的同时返回一次 false,然后重复返回 true.但是在Android WebView中,它反复返回false.

The above code works as expected on a desktop browser and returns false once, then true repeatedly, while a key is held down. However in Android WebView, it repeatedly returns false.

任何想法是什么问题,或处理长键盘按键的替代方法?

Any ideas what the issue is, or an alternative way to handle a long keyboard key press?

更新澄清一下,我试图绑定到长按按键事件而不是忽略它.如果按住某个键一段时间,我想触发一个功能.

Update To clarify, I'm trying to bind to the long press key event rather than ignore it. I want to trigger a function if a key is held down for a duration of time.

推荐答案

您可以重写 document.onkeydown 函数,然后通过抗抖动机制处理相应的键值.毕竟我们需要处理的是键值,而不是长键.代码如下:

You could override the document.onkeydown function, then process the corresponding key value through the anti jitter mechanism. After all, what we need to deal with is the key value, not the long key. The code as follows:

var throttle = function(func, delay) {
            var prev = Date.now();
            return function() {
                var context = this;
                var args = arguments;
                var now = Date.now();
                if (now - prev >= delay) {
                    func.apply(context, args);
                    prev = Date.now();
                }
            }
        }
function onkeypress(evt) {
    var keycode = evt.which ? evt.which : evt.keyCode;
    console.log(keycode);
}

document.onkeypress = throttle(onkeypress, 500);

这篇关于Android WebView 中的键盘长按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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