I take what the user typed using event.which on a keypress, and output using String.fromCharCode.
User types: a
event.which: 67
Outputs: A
For numbers and letters I can handle, but when talking about special characters, I get totally different outputs.
User types: -
event.which: 189
Outputs: ½
After a research, I came across the function charCodeAt, and using this one, the output es perfectly, even special characters.
Unfortunately, I can't use charCodeAt because the user types directly from the $(document), and not from a field.
So, the question is, is there a way I can get the right charCode from the keyPress event.which?
If still can't figure out my doubt, I made you a Fiddle =)
I take what the user typed using event.which on a keypress, and output using String.fromCharCode.
User types: a
event.which: 67
Outputs: A
For numbers and letters I can handle, but when talking about special characters, I get totally different outputs.
User types: -
event.which: 189
Outputs: ½
After a research, I came across the function charCodeAt, and using this one, the output es perfectly, even special characters.
Unfortunately, I can't use charCodeAt because the user types directly from the $(document), and not from a field.
So, the question is, is there a way I can get the right charCode from the keyPress event.which?
If still can't figure out my doubt, I made you a Fiddle =)
Share Improve this question asked May 24, 2012 at 20:27 BernaMarianoBernaMariano 8662 gold badges9 silver badges27 bronze badges 2- I'm unclear what you meant when you explained why you can't use charCodeAt. – kinakuta Commented May 24, 2012 at 20:32
- Because I have to get what the user typed from ANYWHERE on the document, not only inside a field – BernaMariano Commented May 24, 2012 at 20:35
2 Answers
Reset to default 7Use the keypress
event instead of keyup
. It reports character code instead of key codes, and it triggers when actual characters are typed, not when a key is released, so it handles repeated characters too.
$('#field').focus().keypress(function(e){
var key = e.which;
$("#key").val(String.fromCharCode(key));
});
http://jsfiddle/Guffa/QCHt7/1/
Edit:
If you want to catch keypresses everywhere, you have to hook up the event on the document, and also on any elements that consumes keypresses. A textbox for example will handle the keypress and won't let it bubble up to the parent element.
You are using keyup, an event that reports which key was pressed, and not which character was entered. You should use keypress instead, like so:
$(document).keyup(function(e){
console.log(String.fromCharCode(e.which));
});
From jquery doc on keypress:
Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.