Polyfill for KeyboardEvent.prototype.key
.
Firefox has already shipped with this for a while, but this will normalise it for other browsers (e.g., Chrome).
Say goodbye to this:
document.addEventListener('keydown', function (e) {
console.log('Code of key pressed:', e.which || e.keyCode); // 39
});
And hello to this:
document.addEventListener('keydown', function (e) {
console.log('Name of key pressed:', e.key); // ArrowRight
});
Just drop the script on your page and call the polyfill
method.
<script src="index.js"></script>
<script>keyboardeventKeyPolyfill.polyfill();</script>
If you're using AMD:
require('keyboardevent-key-polyfill').polyfill();
Install from npm:
npm install keyboardevent-key-polyfill
Then require the CommonJS module for use with Browserify/WebPack:
require('keyboardevent-key-polyfill').polyfill();