Skip to content

apparently keyCode is obsolete, and has been for a while #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Pomax opened this issue Jun 23, 2017 · 7 comments
Closed

apparently keyCode is obsolete, and has been for a while #81

Pomax opened this issue Jun 23, 2017 · 7 comments

Comments

@Pomax
Copy link
Contributor

Pomax commented Jun 23, 2017

I just read through https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode which highlights that event.keyCode is actually obsolete and has been replaced by event.code instead, with https://w3c.github.io/uievents/#idl-keyboardeventinit having this to say:

Legacy keyboard event implementations include three additional attributes, keyCode, charCode, and which. The keyCode attribute indicates a numeric value associated with a particular key on a computer keyboard, while the charCode attribute indicates the ASCII value of the character associated with that key (which might be the same as the keyCode value) and is applicable only to keys that produce a character value.

In practice, keyCode and charCode are inconsistent across platforms and even the same implementation on different operating systems or using different localizations. This specification does not define values for either keyCode or charCode, or behavior for charCode. In conforming UI Events implementations, content authors can instead use key and code.

It might be necessary to update the code to make use of .code instead, in order to keep up with the DOM spec.

@i-like-robots
Copy link
Owner

Thanks for your query @Pomax. Hopefully this has not caused anybody any issues - these things do tend to unofficially converge with specced features... innerText springs to mind as that was eventually standardised!

@Pomax
Copy link
Contributor Author

Pomax commented Jul 3, 2017

I suspect most browsers still support it because it would break all the things if they didn't, but probably worth updating for a next version =)

The .innerText property is a good example too. but my favourite most recent one I can remember is .outerHTML which became standardized when no one was looking. It makes life (for some of us) so much easier.

@i-like-robots
Copy link
Owner

I tried switching from keyCode to code when investigating #84 but unfortunately it doesn't seem to have been implemented in IE nor yet in Edge.

@Pomax
Copy link
Contributor Author

Pomax commented Aug 3, 2017

boo =( (thanks Edge team...)

@ajmas
Copy link
Contributor

ajmas commented Aug 15, 2017

The key seems to be the better attribute, based on: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key .

If we use delimiterChars, then we could do something like:

['Tab', 'Enter', ',']

But that has the risk of causing issues where we really want multi-character delimiters, if there really is a use-case for them.

Whatever solution we come up with, we need to consider:

  • Impact on typing, in the handleKeyDown method
  • Impact on pasting, in the handlePaste method
  • Whether there is a need for multi-character delimiters, which don't reference keys.

@i-like-robots
Copy link
Owner

i-like-robots commented Aug 16, 2017

I think the separation of delimiter keys (IE. pressing tab or enter) and delimiter characters is OK, so long as it is documented clearly. Using KeyboardEvent.key definitely seems like a good step to make in the next major version.

@ajmas
Copy link
Contributor

ajmas commented Sep 10, 2017

Opened issue #94, based on an issue discovered on Android Chrome, which has an unfortunate impact, when it comes to KeyboardEvent.key and onKeyDown in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants