I need to get localized character on keypress
event. For example: on a Czech keyboard I need to get ř not 5 character (key code 53) (see Czech keyboard layout). Is there any other way to get character and to not use text input and reading value? By other words is there any way how to get character from event object respecting current user keyboard layout?
(Added sample code)
<html>
<body>
<script language="JavaScript">
function onLoad() {
console.log(document.getElementById("test"));
document.getElementById("test").onkeydown = function(event) {
console.log("Code: "+event.keyCode+"; Key: "+String.fromCharCode(event.keyCode));
}
}
</script>
<body onLoad="onLoad();">
<input id="test">
</body>
</html>
(Example online)
When you try this code, and you press ř-key you will see in Firebug console "Code: 53; Key: 5". And I need to get "ř" not "5".
I think that this "problem" appears on all non-English keyboards.
Here, we are using three methods to detect keypress in Python read_key (), is_pressed () and on_press_key (). The read_key () will read which key a user has pressed on the keyboard, and if it’s that key which you wanted, in this case, p, it will print the message You pressed p. The read_key () function returns a character.
An onkeypress event executes JavaScript when a user presses a key. The keyCode property returns the Unicode char code of the key that pressed the onkeypress event. Suggestion: If you only want to detect whether the user has pressed a key, use the onkeydown event instead, because it works for all key types.
If the user presses the key that matches the key specified as the first parameter of the on_press_key () function, it will only execute the function you have passed in as the second parameter. The pynput module is used to detect and control input devices, mainly mouse and keyboard.
An onkeypress event executes JavaScript when a user presses a key. The keyCode property returns the Unicode char code of the key that pressed the onkeypress event. It can use in many form
Using the keypress
event will give you the character typed, regardless of keyboard layout.
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
var charTyped = String.fromCharCode(charCode);
alert("Character typed: " + charTyped);
};
Here's my usual link to Jan Wolter's excellent page documenting JavaScript key handling: http://unixpapa.com/js/key.html
And make sure you are using 'keypress', but not 'keydown' event.
If you use 'keydown' String.fromCharCode(event.keyCode || event.which)
, it won't return you a localised character. At least this was an issue for me on OS X with Chrome / FF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With