Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycode is always zero in Chrome for Android

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.

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

like image 376
barrrrrrrrrooo Avatar asked Jun 17 '13 00:06

barrrrrrrrrooo


People also ask

Why is KeyCode deprecated?

KeyCode was deprecated because in practice it was “inconsistent across platforms and even the same implementation on different operating systems or using different localizations.” The new recommendation is to use key or code .

What is KeyCode of dot?

According to the website, key-code for . is 190.


1 Answers

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) } 
like image 78
coder Avatar answered Oct 02 '22 05:10

coder