See
KeyboardEvent.keyCode - Web APIs | MDN[
^]:
Quote:
You should avoid using this if possible; it's been deprecated for some time. Instead, you should use KeyboardEvent.code, if it's implemented. Unfortunately, some browsers still don't have it, so you'll have to be careful to make sure you use one which is supported on all target browsers. Google Chrome and Safari have implemented KeyboardEvent.keyIdentifier, which was defined in a draft specification but not the final spec.
See also the example code at the link which checks for the availability of the various event members. I don't know why that is not checking first for the recommended
KeyboardEvent.code - Web APIs | MDN[
^] property which should work with recent Mozilla browsers.
But note that returned values differ for the property itself (with the
code
property), the used browser, and the operating system. You would have to handle all these cases. So the best solution would be to avoid using key codes at all.