According to MDN, we should most definitely not be using the .keyCode property. It is deprecated:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
On W3 school, this fact is played down and there is only a side note saying that .keyCode
is provided for compatibility only and that the latest version of the DOM Events Specification recommend using the .key
property instead.
The problem is that .key
is not supported by browsers, so what should we using? Is there something I'm missing?
For instance if you want to detect whether the "Enter"-key was clicked or not:
Instead of
Do it like
MDN has already provided a solution:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
TLDR: I'd suggest that you should use the new
event.key
andevent.code
properties instead of the legacy ones. IE and Edge have support for these properties, but don't support the new key names yet. For them, there is a small polyfill which makes them output the standard key/code names:https://github.com/shvaikalesh/shim-keyboard-event-key
I came to this question searching for the reason of the same MDN warning as OP. After searching some more, the issue with
keyCode
becomes more clear:The problem with using
keyCode
is that non-English keyboards can produce different outputs and even keyboards with different layouts can produce inconsistent results. Plus, there was the case ofIf you read the W3C spec: https://www.w3.org/TR/uievents/#interface-keyboardevent
It goes into some depth describing what was wrong with
keyCode
: https://www.w3.org/TR/uievents/#legacy-key-attributesSo, after establishing the reason why the legacy keyCode was replaced, let's look at what you need to do today:
key
andcode
).You have three ways to handle it, as it's written on the link you shared.
you should contemplate them, that's the right way if you want cross browser support.
It could be easier if you implement something like this.
In addition that all of keyCode, which, charCode and keyIdentifier are deprecated :
charCode
andkeyIdentifier
are non-standard features.keyIdentifier
is removed as of Chrome 54 and Opera 41.0keyCode
returns 0, on keypress event with normal characters on FF.The key property :
As of the time of this writing, the
key
property is supported by all major browsers as of : Firefox 52, Chrome 55, Safari 10.1, Opera 46. Except Internet Explorer 11 which has : non-standard key identifiers and incorrect behavior with AltGraph. More infoIf that is important and/or backward compatibility is, then you can use feature detection as in the following code :
Notice that the
key
value is different fromkeyCode
orwhich
properties in that : it contains the name of the key not its code. If your program needs characters' codes then you can make use ofcharCodeAt()
. For single printable characters you can usecharCodeAt()
, if you're dealing with keys whose values contains multiple characters like ArrowUp chances are : you are testing for special keys and take actions accordingly. So try implementing a table of keys' values and their corresponding codescharCodeArr["ArrowUp"]=38
,charCodeArr["Enter"]=13
,charCodeArr[Escape]=27
... and so on, please take a look at Key Values and their corresponding codesMay be you want to consider forward compatibility i.e use the legacy properties while they're available, and only when dropped switch to the new ones :
See also : the
KeyboardEvent.code
property docs and some more details in this answer.