Skip to content
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

Update use of event.keyCode #14

Open
robotdan opened this issue Jan 29, 2019 · 0 comments
Open

Update use of event.keyCode #14

robotdan opened this issue Jan 29, 2019 · 0 comments

Comments

@robotdan
Copy link
Member

event.keyCode is deprecated.

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode

Example usage to allow for backwards compatibility from above link:

Order of preference:

  1. event.key
  2. event.keyIdentifier
  3. event.keyCode
window.addEventListener("keydown", function (event) {
  if (event.defaultPrevented) {
    return; // Should do nothing if the default action has been cancelled
  }

  var handled = false;
  if (event.key !== undefined) {
    // Handle the event with KeyboardEvent.key and set handled true.
  } else if (event.keyIdentifier !== undefined) {
    // Handle the event with KeyboardEvent.keyIdentifier and set handled true.
  } else if (event.keyCode !== undefined) {
    // Handle the event with KeyboardEvent.keyCode and set handled true.
  }

  if (handled) {
    // Suppress "double action" if event handled
    event.preventDefault();
  }
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant