HTML onkeydown - which key was pressed?

A simple question: when I use the onkeydown event in HTML, how do I know which key was pressed? I am sure it is very easy, but I tried looking for it; w3schools didn't help, and all other results were people asking about specific browsers. Is there a way that works in all major browsers?

Thanks!

+4
source share
2 answers

The event you are looking for is a keydown event , and it provides you with the following properties

  • char DOMString (string) The character value of the key. If the key matches the character to be printed, this value is a non-empty Unicode string containing that character. If the key does not have a printable representation, this is an empty string. See Key names and char values โ€‹โ€‹for details. Only reading. Note. If the key is used as a macro that inserts several characters, this attribute value is a whole 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 value matches the char attribute. Otherwise, this is one of the lines of key values โ€‹โ€‹specified in {{anch ("Key Values")}}. If the key cannot be identified, this is the string "Unidentified". See Key names and char values โ€‹โ€‹for details. Only for reading.

  • charCode Obsolete Unsigned long (int) Unicode key reference number; this attribute is only used for a keypress event. For keys, the char attribute contains several characters; this is the Unicode value of the first character in this attribute. Only for reading.
    Warning: this attribute is deprecated; you should use char if possible.

  • keyCode Deprecated Unsigned long (int) System and implementation-dependent numeric code that defines the unchanged value of a pressed key. This is usually an ASCII decimal code ({{RFC (20)}}) or Windows 1252 corresponding to a key; see {{anch ("Virtual Key Codes")}} for a list of common values. If the key cannot be identified, this value is 0. Read only.
    Warning: this attribute is deprecated; you should use the key, if available.

  • which Deprecated Unsigned long (int) System and implementation-dependent numeric code that defines the unchanged value of the pressed key; this is usually the same as keyCode. Only for reading.
    Warning: this attribute is deprecated; you should use the key, if available.

Since the implementation is incompatible, you need to use the logical OR || to get one that is supported by the client browser.

+6
source

Usually you want to avoid setting events in html. but if I had to guess, your code would look something like this:

 <tag onkeydown="javascript:function_name()"> 

Having your actual code will 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 print the key you pressed. for your own reference.

0
source

Source: https://habr.com/ru/post/1485673/


All Articles