I need to figure out which character was typed into a text field from within the handler that is called by jQuery's keydown
function. key.which
gives me only the keycode, but I need to figure out which ASCII character key
represents. How do I do this?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
The
keyPress
event is what you need to get which character was entered. (See below workaround for keydown event).keydown
andkeyup
provide a code indicating which key is pressed, whilekeypress
indicates which character was entered.Using jQuery
e.which
you can get the key code and using String.fromCharCode you can get the specific character that was pressed (including shiftKey).DEMO: http://jsfiddle.net/9TyzP/3
Code:
Workaround for
keydown
However you can write a simple workaround to get the pressed character working on
keydown
.. The workaround is to create an object with key as the charCode without shift keypress and the value is with shift key.Note: As @Sajjan Sarkar pointed out there are some differences in
e.which
keycode value returned from different browser. Read more hereUpdated the DEMO code to normalize the cross browser keyCode value. Tested and verified in IE 8, FF and Chrome.
Full Code below and updated DEMO: http://jsfiddle.net/S2dyB/17/
More about keyboard events --
The keydown, keypress and keyup events fire when the user presses a key.
keydown Fires when the user depresses a key. It repeats while the user keeps the key depressed.
keypress Fires when an actual character is being inserted in, for instance, a text input. It repeats while the user keeps the key depressed.
keyup Fires when the user releases a key, after the default action of that key has been performed.
When a key is first depressed, the
keydown
event is sent. If the key is not a modifier key, thekeypress
event is sent. When the user releases the key, thekeyup
event is sent.When a key is pressed and held down, it begins to auto-repeat. This results in a sequence of events similar to the following being dispatched:
DEMO: http://jsfiddle.net/9TyzP/1/
keyup, keydown vs keypress
The keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed.
DEMO: http://jsfiddle.net/9TyzP/
References:
https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
http://www.quirksmode.org/dom/events/keys.html
http://unixpapa.com/js/key.html
Selvakumar Arumugam's answer works like a charm for me...until I test numpad. So a minor update here:
http://jsfiddle.net/S2dyB/78/
I do this. It will just ignore the keypress if the value is not a number. Seems to work without any problems...
I created and use the above javascript class for converting gr to en characters. It is able to be used for more languages. It uses JQuery for changing the value pressed from user.
Example,
For character input, it is suggested you use
keypress()
, which will report the actual ASCII code for the character pressed. It automatically takes care of letter case, and ignores non-character presses. In either case, you can use fromCharCode() to convert to a string representation. E.g.Just remember that for
keydown()
andkeyup()
, you'll have to keep track of the case using thee.shiftKey
state.