prime.js icon indicating copy to clipboard operation
prime.js copied to clipboard

Update use of event.keyCode

Open robotdan opened this issue 6 years ago • 0 comments

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);

robotdan avatar Jan 29 '19 21:01 robotdan