PDA

View Full Version : differing keyCode reported for onkeydown & onkeypress



biot023
May 16th, 2006, 10:29 AM
Hallo -- I have two text fields, each of them reports in an alert what key has just been pressed. The first reports on the handle onkeydown, the second on onkeypress, and they report different results. So, for the forward slash key (UK keyboard) I get:
onkeydown: 191
onkeypress: 47

Does anyone know of a method that would allow me to get the same result as I get for the onkeypress when I try onkeydown?
Cheers,
doug.

liorean
May 16th, 2006, 11:43 AM
Well they have different purposes. The keypress event tell you what text has been input through the keyboard. The keydown event tells you what key has been pushed. But the same key can enter more than one different character, a character that can be repeated while holding the button pressed etc.

Let's go through how the browsers hande this though:




iew:
keypress
- ev.metaKey, ev.which and ev.charCode unsupported.
- ev.shiftKey, ev.altKey, ev.ctrlKey correctly supported.
- ev.keyCode contains the character code if resulting in text input, and the key code if not. (Note: iew does not trigger keypress events for text navigation.)

keydown/keyup
- ev.metaKey, ev.which and ev.charCode unsupported.
- ev.shiftKey, ev.altKey, ev.ctrlKey correctly supported.
- ev.keyCode contains the key code.


moz:
keypress
- ev.shiftKey, ev.altKey, ev.ctrlKey, ev.metaKey correctly supported.
- ev.charCode contains the character code if resulting in text input, is empty otherwise.
- ev.keyCode is empty if resulting in text input, and contains the key code otherwise.
- ev.which contains the character code if resulting in text input, and the key code otherwise.

keydown/keyup
- ev.shiftKey, ev.altKey, ev.ctrlKey, ev.metaKey correctly supported
- ev.charCode is empty.
- ev.keyCode contains the key code.
- ev.which contains the key code.


saf:
keypress
- ev.shiftKey, ev.altKey, ev.ctrlKey, ev.metaKey correctly supported.
- ev.charCode contains the character code if resulting in text input, and a bogus value otherwise.
- ev.keyCode contains the character code if resulting in text input, and a bogus value otherwise.
- ev.which contains either the character code if resulting in text input or a bogus value otherwise.

keydown/keyup
- ev.shiftKey, ev.altKey, ev.ctrlKey, ev.metaKey correctly supported.
- ev.charCode contains the character code if resulting in text input, and a bogus value otherwise.
- ev.keyCode contains the key code.
- ev.which contains the key code.


op:
keypress
- ev.charCode, ev.which are unsupported.
- ev.shiftKey, ev.altKey correctly supported.
- ev.ctrlKey, ev.metaKey incorrectly implemented.
- ev.keyCode contains the character code if resulting in text input, and the key code if not.

keydown/keyup
- ev.charCode, ev.which are unsupported.
- ev.shiftKey, ev.altKey correctly supported.
- ev.ctrlKey, ev.metaKey incorrectly implemented.
- ev.keyCode contains the key code.

biot023
May 16th, 2006, 12:09 PM
Excellent reply -- thanks alot for that.
I can see I shall have to rethink my strategy...
You've saved me alot of frustrating hacking!
Cheers,
doug.

Davide Zanotti
May 16th, 2006, 05:00 PM
Excellent reply -- thanks alot for that.

I agree, thanks liorean for the patience of write all these informations :thumbsup: