admin管理员组文章数量:1026373
I'm handling the keyup event, and need to get the character being typed. When I type a dot, data.keyCode is 190, but when I'm trying to convert it using String.fromCharCode(190), I'm getting the "¾" symbol.
This reference tells me 190 is the period symbol. That one says I don't have any chance for cross-browser keypress handling. So, how do I handle this properly?
I'm handling the keyup event, and need to get the character being typed. When I type a dot, data.keyCode is 190, but when I'm trying to convert it using String.fromCharCode(190), I'm getting the "¾" symbol.
This reference tells me 190 is the period symbol. That one says I don't have any chance for cross-browser keypress handling. So, how do I handle this properly?
Share Improve this question asked Oct 2, 2011 at 12:33 uluulu 6,1124 gold badges46 silver badges53 bronze badges 2- Do you want to get only typed character code using keyUp or wants to perform some action/validation once you get code – Vikas Naranje Commented Oct 2, 2011 at 12:48
- I just want to send it to the server. – ulu Commented Oct 2, 2011 at 15:12
1 Answer
Reset to default 5I'm handling the keyup event, and need to get the character being typed.
That's not reliably available. On keyup
you only get the keyCode
and not the charCode
.
(If you think about it this makes sense: the charCode
is associated with the character being typed in a keypress, which might be shifted with a modifier key and so generate a different character to the base key; on keyup
and keydown
, since those events are not immediately associated with a new character being typed, there isn't a known character to generate.)
So if you are doing something that relies on knowing whether a .
character was typed, you will need to use the keypress
event instead.
If you want to know when the abstract key that is labelled .
/>
on some mon keyboard layouts is released, you can do that with keyup
, using the keyCode
listed on the unixpapa link. Using keyCode
like this is typically used for games and other features where the actual key position matters rather than the character it generates when typed.
Unfortunately the browsers have made an inconsistent mess of some keyCodes including this one, so you'd have to check it for all possible values listed above, and you can't reliably tell the difference between the main keyboard period, the numpad period/delete key and the main delete key.
I'm handling the keyup event, and need to get the character being typed. When I type a dot, data.keyCode is 190, but when I'm trying to convert it using String.fromCharCode(190), I'm getting the "¾" symbol.
This reference tells me 190 is the period symbol. That one says I don't have any chance for cross-browser keypress handling. So, how do I handle this properly?
I'm handling the keyup event, and need to get the character being typed. When I type a dot, data.keyCode is 190, but when I'm trying to convert it using String.fromCharCode(190), I'm getting the "¾" symbol.
This reference tells me 190 is the period symbol. That one says I don't have any chance for cross-browser keypress handling. So, how do I handle this properly?
Share Improve this question asked Oct 2, 2011 at 12:33 uluulu 6,1124 gold badges46 silver badges53 bronze badges 2- Do you want to get only typed character code using keyUp or wants to perform some action/validation once you get code – Vikas Naranje Commented Oct 2, 2011 at 12:48
- I just want to send it to the server. – ulu Commented Oct 2, 2011 at 15:12
1 Answer
Reset to default 5I'm handling the keyup event, and need to get the character being typed.
That's not reliably available. On keyup
you only get the keyCode
and not the charCode
.
(If you think about it this makes sense: the charCode
is associated with the character being typed in a keypress, which might be shifted with a modifier key and so generate a different character to the base key; on keyup
and keydown
, since those events are not immediately associated with a new character being typed, there isn't a known character to generate.)
So if you are doing something that relies on knowing whether a .
character was typed, you will need to use the keypress
event instead.
If you want to know when the abstract key that is labelled .
/>
on some mon keyboard layouts is released, you can do that with keyup
, using the keyCode
listed on the unixpapa link. Using keyCode
like this is typically used for games and other features where the actual key position matters rather than the character it generates when typed.
Unfortunately the browsers have made an inconsistent mess of some keyCodes including this one, so you'd have to check it for all possible values listed above, and you can't reliably tell the difference between the main keyboard period, the numpad period/delete key and the main delete key.
本文标签: javascriptGetting the character from keyup eventStack Overflow
版权声明:本文标题:javascript - Getting the character from keyup event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745643787a2160904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论