Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for extended Function keys F13 to F24 #56

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
components
build
node_modules
.idea/
package-lock.json
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ declare interface CodesMap {
'f10': number;
'f11': number;
'f12': number;
'f13': number;
'f14': number;
'f15': number;
'f16': number;
'f17': number;
'f18': number;
'f19': number;
'f20': number;
'f21': number;
'f22': number;
'f23': number;
'f24': number;
'numpad 0': number;
'numpad 1': number;
'numpad 2': number;
Expand Down
40 changes: 20 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
function keyCode(searchInput) {
// Keyboard Events
if (searchInput && 'object' === typeof searchInput) {
var hasKeyCode = searchInput.which || searchInput.keyCode || searchInput.charCode
var hasKeyCode = searchInput.which || searchInput.keyCode || searchInput.charCode;
if (hasKeyCode) searchInput = hasKeyCode
}

// Numbers
if ('number' === typeof searchInput) return names[searchInput]
if ('number' === typeof searchInput) return names[searchInput];

// Everything else (cast to string)
var search = String(searchInput)
var search = String(searchInput);

// check codes
var foundNamedKey = codes[search.toLowerCase()]
if (foundNamedKey) return foundNamedKey
var foundNamedKey = codes[search.toLowerCase()];
if (foundNamedKey) return foundNamedKey;

// check aliases
var foundNamedKey = aliases[search.toLowerCase()]
if (foundNamedKey) return foundNamedKey
var foundNamedKey = aliases[search.toLowerCase()];
if (foundNamedKey) return foundNamedKey;

// weird character?
if (search.length === 1) return search.charCodeAt(0)
if (search.length === 1) return search.charCodeAt(0);

return undefined
}
Expand All @@ -46,22 +46,22 @@ function keyCode(searchInput) {
*/
keyCode.isEventKey = function isEventKey(event, nameOrCode) {
if (event && 'object' === typeof event) {
var keyCode = event.which || event.keyCode || event.charCode
var keyCode = event.which || event.keyCode || event.charCode;
if (keyCode === null || keyCode === undefined) { return false; }
if (typeof nameOrCode === 'string') {
// check codes
var foundNamedKey = codes[nameOrCode.toLowerCase()]
var foundNamedKey = codes[nameOrCode.toLowerCase()];
if (foundNamedKey) { return foundNamedKey === keyCode; }

// check aliases
var foundNamedKey = aliases[nameOrCode.toLowerCase()]
var foundNamedKey = aliases[nameOrCode.toLowerCase()];
if (foundNamedKey) { return foundNamedKey === keyCode; }
} else if (typeof nameOrCode === 'number') {
return nameOrCode === keyCode;
}
return false;
}
}
};

exports = module.exports = keyCode;

Expand Down Expand Up @@ -115,7 +115,7 @@ var codes = exports.code = exports.codes = {
'\\': 220,
']': 221,
"'": 222
}
};

// Helper aliases

Expand All @@ -140,34 +140,34 @@ var aliases = exports.aliases = {
'ins': 45,
'del': 46,
'cmd': 91
}
};

/*!
* Programatically add the following
*/

// lower case chars
for (i = 97; i < 123; i++) codes[String.fromCharCode(i)] = i - 32
for (let i = 97; i < 123; i++) codes[String.fromCharCode(i)] = i - 32;

// numbers
for (var i = 48; i < 58; i++) codes[i - 48] = i
for (let i = 48; i < 58; i++) codes[i - 48] = i;

// function keys
for (i = 1; i < 13; i++) codes['f'+i] = i + 111
for (let i = 1; i <= 24; i++) codes['f' + i] = i + 111;

// numpad keys
for (i = 0; i < 10; i++) codes['numpad '+i] = i + 96
for (let i = 0; i < 10; i++) codes['numpad '+i] = i + 96;

/**
* Get by code
*
* exports.name[13] // => 'Enter'
*/

var names = exports.names = exports.title = {} // title for backward compat
var names = exports.names = exports.title = {}; // title for backward compat

// Create reverse mapping
for (i in codes) names[codes[i]] = i
for (let i in codes) names[codes[i]] = i;

// Add aliases
for (var alias in aliases) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "keycode",
"description": "Convert between keyboard keycodes and keynames and vice versa.",
"version": "2.2.0",
"version": "2.2.1",
"main": "index.js",
"directories": {
"example": "examples",
Expand All @@ -12,16 +12,16 @@
},
"repository": {
"type": "git",
"url": "git://github.com/timoxley/keycode.git"
"url": "git://github.com/get-wrecked/keycode.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/timoxley/keycode/issues"
"url": "https://github.com/get-wrecked/keycode/issues"
},
"devDependencies": {
"mocha": "^3.0.2"
},
"homepage": "https://github.com/timoxley/keycode",
"homepage": "https://github.com/get-wrecked/keycode",
"dependencies": {},
"keywords": [
"keyboard",
Expand Down