Skip to content

donmccurdy/keyboardevent-key-polyfill

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

polyfill for `KeyboardEvent.prototype.key`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 71.2%
  • HTML 28.8%