admin管理员组文章数量:1287785
A simple question: when I use the onkeydown
event in HTML, how can I find out what key was pressed? I'm sure it's really easy, but I've tried googling it; w3schools was no help, and all the other results were people asking about specific browsers. Is there no way which works in all major browsers?
Thanks!
A simple question: when I use the onkeydown
event in HTML, how can I find out what key was pressed? I'm sure it's really easy, but I've tried googling it; w3schools was no help, and all the other results were people asking about specific browsers. Is there no way which works in all major browsers?
Thanks!
Share Improve this question edited Jun 11, 2013 at 18:53 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jun 11, 2013 at 18:43 user2226001user2226001 991 silver badge8 bronze badges 1- stackoverflow./questions/1648130/keycode-on-keydown-event – Ivan Chernykh Commented Jun 11, 2013 at 18:49
2 Answers
Reset to default 8The event you're looking for is the keydown Event, and it provides you with the following properties
char
DOMString (string) The character value of the key. If the key corresponds to a printable character, this value is a non-empty Unicode string containing that character. If the key doesn't have a printable representation, this is an empty string. See key names and char values for the detail. Read only. Note: If the key is used as a macro that inserts multiple characters, this attribute's value is the entire string, not just the first character.key
DOMString (string) The key value of the key represented by the event. If the value has a printed representation, this attribute's value is the same as the char attribute. Otherwise, it's one of the key value strings specified in {{ anch("Key values") }}. If the key can't be identified, this is the string "Unidentified". See key names and char values for the detail. Read Only.charCode
Deprecated Unsigned long (int) The Unicode reference number of the key; this attribute is used only by the keypress event. For keys whose char attribute contains multiple characters, this is the Unicode value of the first character in that attribute. Read only.
Warning: This attribute is deprecated; you should use char instead, if available.keyCode
Deprecated Unsigned long (int) A system and implementation dependent numerical code identifying the unmodified value of the pressed key. This is usually the decimal ASCII ({{ RFC(20) }}) or Windows 1252 code corresponding to the key; see {{ anch("Virtual key codes") }} for a list of mon values. If the key can't be identified, this value is 0. Read only.
Warning: This attribute is deprecated; you should use key instead, if available.which
Deprecated Unsigned long (int) A system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as keyCode. Read only.
Warning: This attribute is deprecated; you should use key instead, if available.
As implementation is inconsistant, you'll need to use a logical OR ||
to get the one supported by the client's browser.
Typically you want to avoid setting events in the html. but if I had to take a guess your code is somewhat like this:
<tag onkeydown="javascript:function_name()">
Having your actual code would help direct this a bit more but this is the next step.
function function_name(event){
console.log(event.key) //or event.keyCode / event.which
}
This should output the key you pressed. for your own reference.
本文标签: javascriptonkeydown in HTMLwhat key was pressedStack Overflow
版权声明:本文标题:javascript - onkeydown in HTML - what key was pressed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741318676a2372075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论