Keycode is always zero in Chrome for Android

后端 未结 10 2049
栀梦
栀梦 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:53

    change the type of the input to tel : <input type="tel">

    this will let you log the keyCode but it doesnt log the backspace, and it might force the keyboard on mobile to only numbers.

    0 讨论(0)
  • 2020-12-05 13:55

    I have faced the same issue with Android Devices of SonyXperia and HTC. I have developing hybrid application where i am validating Amount text field by reading event.keyCode() of the entered char on keydown event from text field, so that i can allow only numbers and dot(.) char to be entered. I tried but it doesn't worked, returning always 0 in these devices. Later i came with other solution with keyup event and char matching through Regular Expression:

    $("#AmountField").keyup(function (e) {
    
        var regex = /^[0-9\.]$/;
        var str = $(this).val();
        var subStr = str.substr(str.length - 1);
        if(!regex.test(subStr)) {
    
            if(str.length >0){
                $(this).val(str.substr(0, (str.length - 1)));
            }else{ 
                $(this).val();
            }
    
        }
    
    });
    

    Hope this can help for the people who facing this issue. Good Luck.

    0 讨论(0)
  • 2020-12-05 13:55
    <input type="text" id="char" size="15" onblur="showKeyCode()" value="a">
    <input type="button" value="Show Key Code" onclick="showKeyCode();">
    <script>
    function showKeyCode()
    {
        var character = document.getElementById ( "char" ).value.substr(this.length - 1);
        var  code = character.charCodeAt();
    
        var stringall = document.getElementById ( "char" ).value;
        var msg = "The Key Code for the \""+character+"\" character is "+code+".";
        alert(msg);
     }
    </script>
    

    For reference

    0 讨论(0)
  • 2020-12-05 13:56

    If anybody still digging it.Problem appears on stock Samsung keyboard for android devices.

    Instead use onkeyup.

    0 讨论(0)
提交回复
热议问题