Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

accept { keyCode }

  • Loading branch information...
commit 405d23b8e59b14a576544076b9bb472b8670eb26 1 parent dba4d5c
@nathan7 nathan7 authored
Showing with 12 additions and 0 deletions.
  1. +4 −0 Readme.md
  2. +3 −0  index.js
  3. +5 −0 test/keycode.js
View
4 Readme.md
@@ -22,6 +22,10 @@ document.addEventListener('keydown', function(e) {
The `keycode` method responds differently depending on whether you're
looking up a keycode or a keyname.
+##keycode(keycode:Object)
+
+Returns the keycode for the given object's .keyCode.
+
##keycode(keycode:Number)
Returns the lowercase title for given numeric keycode.
View
3  index.js
@@ -1,6 +1,8 @@
// Source: http://jsfiddle.net/vWx8V/
// http://stackoverflow.com/questions/5603195/full-list-of-javascript-keycodes
+var has = ({}).hasOwnProperty
+
/**
* Conenience method returns corresponding value for given keyName or keyCode.
*
@@ -9,6 +11,7 @@
* @api public
*/
exports = module.exports = function(searchInput) {
+ if ('object' === typeof searchInput && searchInput && has.call(searchInput, 'keyCode')) searchInput = searchInput.keyCode
if ('number' === typeof searchInput) return names[searchInput]
var search = String(searchInput)
var foundNamedKey = codes[search.toLowerCase()]
View
5 test/keycode.js
@@ -45,6 +45,11 @@ it('returns code for objects implementing toString function', function() {
assert.strictEqual(keycode(obj), 65);
})
+it('returns char for objects with a keyCode property', function() {
+ var obj = { keyCode: 65 }
+ assert.strictEqual(keycode(obj), 'a');
+})
+
it('returns undefined for any other passed in type', function() {
assert.strictEqual(keycode({}), undefined);
assert.strictEqual(keycode([]), undefined);
Please sign in to comment.
Something went wrong with that request. Please try again.