I use onkeydown
event and read pressed key from event
object:
function test(e) {
e = e || window.e;
var keyCode = e.which || e.keyCode;
alert(keyCode +' -> '+ String.fromCharCode(keyCode));
}
Above function works weird if user press backslash key (\
) - it always return Ü
instead of \
. Value of e.keyCode
is 220
what means that everything is ok, so probably fromCharCode()
is an issue.
I think that the problem is a fact that backslash is a special character. But how can I omit it and display proper char for 220
code?
I have made live example on JSFiddle.
Problem here is that you try to match keyboard key code to actual letter of keyboard layout.
E.g (test keyboard key code):
2
and key code is50
č
, but key code is still50
.You should have some some letter-to-key-code map, but you will never match all keyboard layouts.