在Android版Chrome中,键码始终为零 [英] keycode is always zero in Chrome for Android

查看:126
本文介绍了在Android版Chrome中,键码始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的网站上检测自定义搜索框的键码,但Chrome上的键码始终返回为零(除退格键返回8外)。有没有其他人经历过这个,你是如何解决它的?我们的网站适用于除Chrome之外的所有移动浏览器,因为我们无法检测到非零的键码或charcode。

I need to detect the keycode for a custom search box on my website, but the keycode always returns as zero on Chrome for Android (except for backspace, which returns 8). Has anyone else experienced this, and how did you get around it? Our website works on all mobile browsers except Chrome for Android because we can't detect a non-zero keycode or charcode.

我在Android上运行Chrome 27.0.1453.90 4.1.2果冻豆。这个问题可以通过如下简单的东西来复制:

alert(event.keyCode);

I'm running Chrome 27.0.1453.90 on Android 4.1.2 Jelly Bean. The problem can be duplicated with something as simple as:
alert(event.keyCode);

推荐答案

以下解决方案 也为我工作。可能对其他人也有用。

below solution also work for me. might be useful for others also.

var getKeyCode = function (str) {
    return str.charCodeAt(str.length - 1);
}

document.getElementById("a").onkeyup = function (e) {
    var kCd = e.keyCode || e.which;
    if (kCd == 0 || kCd == 229) { //for android chrome keycode fix
        kCd = getKeyCode(this.value);
    }
    alert(kCd)
}

这篇关于在Android版Chrome中,键码始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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