admin管理员组文章数量:1331849
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.
本文标签: jqueryJavascript eventwhich and charCodeAt working togetherStack Overflow
版权声明:本文标题:jquery - Javascript: event.which and charCodeAt working together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742258131a2442005.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论