最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keycode value is return as 229 for all keys - Stack Overflow

programmeradmin1浏览0评论

I have run the normal textbox in android device an i have face some issues which is mentioned below.

1.Keypress event does not triggered in android device 2.keycode value always return as 229 only

How to resolve this issue?

I have run the normal textbox in android device an i have face some issues which is mentioned below.

1.Keypress event does not triggered in android device 2.keycode value always return as 229 only

How to resolve this issue?

Share Improve this question asked Aug 19, 2016 at 9:17 Roy thomasRoy thomas 1151 gold badge1 silver badge10 bronze badges 4
  • 2 Possible duplicate of Capture keys typed on android virtual keyboard using javascript – billyonecan Commented Aug 19, 2016 at 9:33
  • Hi billyonecan Same issue is for me,Thanks But i cant find any solution from that.Can you please explain what solution is given there? – Roy thomas Commented Aug 19, 2016 at 9:54
  • You can check this stackoverflow./questions/25043934/… – flymaxelib Commented Sep 8, 2021 at 8:46
  • Try in Firefox browser in Android devices. – Nice Books Commented Sep 8, 2021 at 10:16
Add a ment  | 

2 Answers 2

Reset to default 4

Normal keypress event does not give keyCode in android device. 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.

$('#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.

<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>
发布评论

评论列表(0)

  1. 暂无评论