keyboard-key
Advanced tools
Comparing version 1.0.2 to 1.0.4
{ | ||
"name": "keyboard-key", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"description": "A simple utility for determining the KeyboardEvent.key property from a keyboard event.", | ||
@@ -5,0 +5,0 @@ "main": "src/keyboardKey.js", |
keyboard-key | ||
[![CircleCI](https://img.shields.io/circleci/project/github/levithomason/keyboard-key.svg?style=flat-square)](https://circleci.com/gh/levithomason/keyboard-key) | ||
[![CircleCI](https://img.shields.io/circleci/project/github/levithomason/keyboard-key/master.svg?style=flat-square)](https://circleci.com/gh/levithomason/keyboard-key/master) | ||
[![Codecov](https://img.shields.io/codecov/c/github/levithomason/keyboard-key/master.svg?style=flat-square)](https://codecov.io/gh/levithomason/keyboard-key) | ||
@@ -17,2 +17,3 @@ [![David](https://img.shields.io/david/levithomason/keyboard-key.svg?style=flat-square)](https://david-dm.org/levithomason/keyboard-key) | ||
- [Why?](#why) | ||
- [Locale Caveat](#locale-caveat) | ||
@@ -86,4 +87,8 @@ <!-- tocstop --> | ||
## Locale Caveat | ||
This utility interprets use of the shift key when inferring event `key` values. Example, an event describing <kbd>shift</kbd>+<kbd>/</kbd> would result in a `key` value of <kbd>?</kbd>. This logic assumes an `en-US` locale keyboard layout. This will **not work** if you are using a different locale such as a German layout where <kbd>/</kbd> is <kbd>shift</kbd>+<kbd>7</kbd>. | ||
[1]: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key | ||
[2]: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values | ||
[3]: http://caniuse.com/#feat=keyboardevent-key | ||
[3]: http://caniuse.com/#feat=keyboardevent-key |
@@ -96,13 +96,13 @@ 'use strict' | ||
* Get the `keyCode` or `which` value from a keyboard event or `key` name. | ||
* @param {string|object} name A keyboard event like object or `key` name. | ||
* @param {string} [name.key] If object, it must have one of these keys. | ||
* @param {string} [name.keyCode] If object, it must have one of these keys. | ||
* @param {string} [name.which] If object, it must have one of these keys. | ||
* @param {string|object} eventOrKey A keyboard event-like object or `key` name. | ||
* @param {string} [eventOrKey.key] If object, it must have one of these keys. | ||
* @param {string} [eventOrKey.keyCode] If object, it must have one of these keys. | ||
* @param {string} [eventOrKey.which] If object, it must have one of these keys. | ||
* @returns {*} | ||
*/ | ||
getCode: function getCode(name) { | ||
if (isObject(name)) { | ||
return name.keyCode || name.which || this[name.key] | ||
getCode: function getCode(eventOrKey) { | ||
if (isObject(eventOrKey)) { | ||
return eventOrKey.keyCode || eventOrKey.which || this[eventOrKey.key] | ||
} | ||
return this[name] | ||
return this[eventOrKey] | ||
}, | ||
@@ -112,15 +112,22 @@ | ||
* Get the key name from a keyboard event, `keyCode`, or `which` value. | ||
* @param {number|object} code A keyboard event like object or key name. | ||
* @param {number} [code.keyCode] If object, it must have one of these keys. | ||
* @param {number} [code.which] If object, it must have one of these keys. | ||
* @param {number} [code.shiftKey] If object, it must have one of these keys. | ||
* @param {number|object} eventOrCode A keyboard event-like object or key code. | ||
* @param {number} [eventOrCode.key] If object with a `key` name, it will be returned. | ||
* @param {number} [eventOrCode.keyCode] If object, it must have one of these keys. | ||
* @param {number} [eventOrCode.which] If object, it must have one of these keys. | ||
* @param {number} [eventOrCode.shiftKey] If object, it must have one of these keys. | ||
* @returns {*} | ||
*/ | ||
getKey: function getKey(code) { | ||
var isEvent = isObject(code) | ||
var name = codes[isEvent ? code.keyCode || code.which : code] | ||
getKey: function getKey(eventOrCode) { | ||
var isEvent = isObject(eventOrCode) | ||
// handle events with a `key` already defined | ||
if (isEvent && eventOrCode.key) { | ||
return eventOrCode.key | ||
} | ||
var name = codes[isEvent ? eventOrCode.keyCode || eventOrCode.which : eventOrCode] | ||
if (Array.isArray(name)) { | ||
if (isEvent) { | ||
name = name[code.shiftKey ? 1 : 0] | ||
name = name[eventOrCode.shiftKey ? 1 : 0] | ||
} else { | ||
@@ -127,0 +134,0 @@ name = name[0] |
@@ -128,3 +128,7 @@ const keyboardKey = require('../src/keyboardKey') | ||
}) | ||
test('handles event like objects with a `key` property', () => { | ||
const keyName = keyboardKey.getKey({ key: '/' }) | ||
expect(keyName).toEqual('/') | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
173478
489
93