8

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?

Roy thomas
  • 115
  • 1
  • 8
  • 2
    Possible duplicate of [Capture keys typed on android virtual keyboard using javascript](http://stackoverflow.com/questions/30743490/capture-keys-typed-on-android-virtual-keyboard-using-javascript) – billyonecan Aug 19 '16 at 09: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 Aug 19 '16 at 09:54
  • You can check this https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229 – flymaxelib Sep 08 '21 at 08:46
  • Try in Firefox browser in Android devices. – Nice Books Sep 08 '21 at 10:16

2 Answers2

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();             
     }
})
Zhming
  • 333
  • 3
  • 12
0

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>
Akshay Tilekar
  • 1,910
  • 2
  • 12
  • 22
  • 4
    This wont work if you type in the middle of the string: imagine this is the input [ ] type in something [something] tap in the middle [some|thing] type "a" [someathing] alert says "g key Pressed" – silva96 Nov 07 '19 at 16:43
  • e.target.value.charAt(e.target.selectionStart - 1).charCodeAt() – Zhming Sep 08 '21 at 08:21