简体   繁体   中英

jQuery check if pressed key is printable

By using jQuery keydown i'm able to get the target and pressed keyCode

I wanted to check it the pressing key is PRINTABLE KEY

So i used e.key , but this doesn't work in all browsers

Results for e.key :

Mozilla FireFox : MozPrintableKey
Google Chome: undefined
IE: undefined

Is there a all browser compatible way to check if pressing key is printable?

You can define a printable key hash list,if you want it work in all browsers

var printableTable = {
  65:a,
  66:b,
  67:c
  ....
}
var el = document.getElementById("id");
el.onkeypress = function(e) {
    e = e || window.event;
    var keyCode = e.keyCode||e.which;
    if (keyCode in printableTable) {
        alert('this is printableTable!')
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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