Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  53] [ 1]  / answers: 1 / hits: 30836  / 13 Years ago, thu, march 24, 2011, 12:00:00

Some desktop apps treat the 'carriage return' key and the numpad's 'enter' key differently. I've noticed that these two keys generate the same keyCode (13) in Javascript (jQuery).



Are they converted to be equal in the browser environment, or is it possible to differentiate between them (ie. make the CR make a new line in a text area, and the 'enter' key submit it's form ?


More From » keycode

 Answers
11

See Jan Wolters’ treatise on Javascript Madness: Keyboard Events.



Enter and Numpad Enter both give the same keycode, i.e. 13, because browsers do not differentiate between the two keys. To be honest, nor do most environments. It is possible to differentiate between them using the Windows API (for example), but it does take extra effort to do so. This, however, falls outside the scope of the browser’s abstraction.



UPDATE



As Bill Thorne rightfully mentions, the KeyboardEvent object sports a location property nowadays.



From the Mozilla Developer Network:




Possible values are:



DOM_KEY_LOCATION_STANDARD 0 The key has
only one version, or can't be distinguished between the left and right
versions of the key, and was not pressed on the numeric keypad or a
key that is considered to be part of the keypad.



DOM_KEY_LOCATION_LEFT 1 The key was the left-hand version of the key;
for example, the left-hand Control key was pressed on a standard 101
key US keyboard. This value is only used for keys that have more that
one possible location on the keyboard.



DOM_KEY_LOCATION_RIGHT 2 The
key was the right-hand version of the key; for example, the right-hand
Control key is pressed on a standard 101 key US keyboard. This value
is only used for keys that have more that one possible location on the
keyboard.



DOM_KEY_LOCATION_NUMPAD 3 The key was on the numeric
keypad, or has a virtual key code that corresponds to the numeric
keypad.



Note: When NumLock is locked, Gecko always returns
DOM_KEY_LOCATION_NUMPAD for the keys on the numeric pad. Otherwise,
when NumLock is unlocked and the keyboard actually has a numeric
keypad, Gecko always returns DOM_KEY_LOCATION_NUMPAD too. On the other
hand, if the keyboard doesn't have a keypad, such as on a notebook
computer, some keys become Numpad only when NumLock is locked. When
such keys fires key events, the location attribute value depends on
the key. That is, it must not be DOM_KEY_LOCATION_NUMPAD. Note:
NumLock key's key events indicate DOM_KEY_LOCATION_STANDARD both on
Gecko and Internet Explorer.



[#93098] Wednesday, March 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
louiemarvinm

Total Points: 473
Total Questions: 103
Total Answers: 94

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
;