Question

Obviously, a web application needs to adjust to the user's keyboard settings, right? Is there a way to tell Dojo to make a connection to the actual KeyPress event instead of KeyDown, so we can get the character typed from event.charCode?

Since we live in an international world, with multiple operating systems and what not, this information is not sufficient to find out what character the user actually typed, unless I have some function built into the browser to ask the operating system.

As an example, on a German keyboard on Linux, [ is reached via Alt Gr-8, which sends a keydown for Alt and then a keydown with [. Fine, just ignore the first part. On a windows system with a German keyboard, the second event is for an 8 with ctrlKey and altKey set true. Which I don't think JavaScript code should interpret hard-coded, because with other keyboard settings, this key combo will actually mean a different character.

As another example (probably not connected to Dojo, but rather a different programmer's glitch, sorry for the ranting …), with a US keyboard on mac, you can't type the German character ß within the outlook web interface – because outlook bogusly(!) hijacks the alt key (which on the mac is exclusively used to modify the characters typed) to trigger actions and alt-s is thus remapped to mean send. Typically in the middle of a word, of course.

Was it helpful?

Solution

You can set the 5th dontFix argument passed to dojo.connect() to true, which tells it to let it pass thru as-is without the special handling. See https://github.com/dojo/dojo/blob/master/_base/connect.js#L32

OTHER TIPS

event.charCode contains the character that was produced from the keyboard press, not the actual key that was pressed.

https://developer.mozilla.org/en/DOM/event.charCode#Notes

In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case

edit: Also see https://developer.mozilla.org/en/Gecko_Keypress_Event, it contains a more insightful explanation as to how charCode works (specifically in Gecko, but some of it applies to other browsers too). You might find this interesting:

... when the currently selected keyboard layout produces a Unicode character (according to the current state of CapsLock and NumLock), the charCode property contains that character

Use the keypress event, whose purpose is to give you information about the character typed by the user. You'll need (bizarrely) the keyCode property in IE and the which property in other browsers; these give you the character code typed.

The definitive page for JavaScript key events: http://unixpapa.com/js/key.html

document.onkeypress = function(e) {
    e = e || window.event;
    var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
    alert(String.fromCharCode(charCode));
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top