polyfill for `KeyboardEvent.prototype.key`
JavaScript HTML
Latest commit c76b036 Apr 8, 2016 @cvan Merge pull request #5 from Gheoan/patch-1
add '  ' key (Space)
Permalink
Failed to load latest commit information.
.gitignore add .gitignore Dec 23, 2015
LICENSE make it a module (fixes #1) Dec 23, 2015
README.md make it a module (fixes #1) Dec 23, 2015
index.html make it a module (fixes #1) Dec 23, 2015
index.js add ' ' key (Space) Feb 11, 2016
package.json 1.0.1 Dec 23, 2015

README.md

keyboardevent-key-polyfill

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

Example

View Demo


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

Usage

From standalone script

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

From npm (Node/Browserify/WebPack)

Install from npm:

npm install keyboardevent-key-polyfill

Then require the CommonJS module for use with Browserify/WebPack:

require('keyboardevent-key-polyfill').polyfill();

License

MIT