简体   繁体   English

所有键的键码值都返回为 229

[英]Keycode value is return as 229 for all keys

I have run the normal textbox in android device an i have face some issues which is mentioned below.我已经在 android 设备中运行了普通的文本框,但我遇到了一些下面提到的问题。

1.Keypress event does not triggered in android device 2.keycode value always return as 229 only 1.Keypress 事件不会在 android 设备中触发 2.keycode 值总是返回为 229 只

How to resolve this issue?如何解决这个问题?

Normal keypress event does not give keyCode in android device.正常的按键事件不会在 android 设备中给出 keyCode。 There has already been a big discussion on this.对此已经有很大的讨论

If you want to capture the press of space bar or special chars , you can use keyup event.如果你想捕捉space barspecial chars的按下,你可以使用keyup事件。

$('#input').on('keyup', e => {
     var keyCode = e.keyCode || e.which;
     if (keyCode == 0 || keyCode == 229) { 
         keyCode = e.target.value.charAt(e.target.selectionStart - 1).charCodeAt();             
     }
})

just check your input characters keyCode, if it is 0 or 229 then here is the function getKeyCode which uses charCodeAt of JS to return the KeyCode which takes input string a parameter and returns keycode of last character.只需检查您输入的字符 keyCode,如果它是 0 或 229,那么这里是函数getKeyCode ,它使用 JS 的charCodeAt返回 KeyCode,它将输入字符串作为参数并返回最后一个字符的 keycode。

<script>
        var getKeyCode = function (str) {
            return str.charCodeAt(str.length);
        }


        $('#myTextfield').on('keyup',function(e){

            //for android chrome keycode fix
            if (navigator.userAgent.match(/Android/i)) {

                var inputValue = this.value;

                var charKeyCode = e.keyCode || e.which;
                if (charKeyCode == 0 || charKeyCode == 229) { 
                    charKeyCode = getKeyCode(inputValue);
                    alert(charKeyCode+' key Pressed');
                }else{
                   alert(charKeyCode+' key Pressed');
                    }
            }       
        });
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM