1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sravis
  • 3,562
  • 6
  • 36
  • 73
  • 1
    you can use the keypress event... it is fired if a printanble character is pressed – Arun P Johny Dec 14 '13 at 11:05
  • Note that a "pressed key" may be printable sometimes and not others. E.g., "V" is printable, but ctrl-V is paste which may or may not add printable characters depending on what the clipboard contains at the time. – nnnnnn Dec 14 '13 at 11:13
  • @ArunPJohny but keypress will not detect non printable keys like tab, control etc.. – sravis Dec 14 '13 at 11:13
  • Now what is "printable"? Different fonts have different characters that able to be rendered and printed. – Qantas 94 Heavy Dec 14 '13 at 11:18
  • Is this link helpful http://stackoverflow.com/questions/4194163/detect-printable-keys – Abhishek Dec 14 '13 at 11:31

1 Answers1

0

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!')
    }
});
yoyo
  • 722
  • 6
  • 4