Keycode is always zero in Chrome for Android

后端 未结 10 2080
栀梦
栀梦 2020-12-05 13:17

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

10条回答
  •  暖寄归人
    2020-12-05 13:49

    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)
    }
    

提交回复
热议问题