Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get LOCALIZED character on keypress?

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.

like image 832
Honza Kuchař Avatar asked Mar 24 '11 13:03

Honza Kuchař


People also ask

How to detect keypress in Python read_key()?

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.

How to detect if a user has pressed a key?

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.

How does on_press_key() work in Python?

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.

What is onkeypress event in JavaScript?

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


2 Answers

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

like image 89
Tim Down Avatar answered Sep 28 '22 10:09

Tim Down


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.

like image 33
Tomas P. R. Avatar answered Sep 28 '22 08:09

Tomas P. R.