├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── index.d.cts ├── index.d.ts ├── index.js ├── package.json └── rollup.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | .tern-* 3 | /index.cjs 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | 3 | rollup.config.js 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2016 by Marijn Haverbeke and others 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # W3C keyname 2 | 3 | Tiny library that exports a function `keyName` that takes a keyboard event and 4 | returns a 5 | [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)-style 6 | string. Will use the actual `key` property of the event if available, 7 | and fall back to a value synthesized from the `keyCode` otherwise. 8 | 9 | Probably often wrong on non-US keyboards, since the correspondence 10 | between a key code and the character it produces when shift is held is 11 | predicted based on a hard-coded table. Meant as a fallback for 12 | `KeyboardEvent.key`, not a replacement. 13 | 14 | The lookup tables from key codes (`event.keyCode`) to names are 15 | exported as `base` (when Shift isn't held) and `shift` (when Shift is 16 | held). 17 | 18 | License: MIT 19 | -------------------------------------------------------------------------------- /index.d.cts: -------------------------------------------------------------------------------- 1 | export function keyName(event: Event): string; 2 | 3 | export const base: {[keyCode: number]: string}; 4 | 5 | export const shift: {[keyCode: number]: string}; 6 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export function keyName(event: Event): string; 2 | 3 | export const base: {[keyCode: number]: string}; 4 | 5 | export const shift: {[keyCode: number]: string}; 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export var base = { 2 | 8: "Backspace", 3 | 9: "Tab", 4 | 10: "Enter", 5 | 12: "NumLock", 6 | 13: "Enter", 7 | 16: "Shift", 8 | 17: "Control", 9 | 18: "Alt", 10 | 20: "CapsLock", 11 | 27: "Escape", 12 | 32: " ", 13 | 33: "PageUp", 14 | 34: "PageDown", 15 | 35: "End", 16 | 36: "Home", 17 | 37: "ArrowLeft", 18 | 38: "ArrowUp", 19 | 39: "ArrowRight", 20 | 40: "ArrowDown", 21 | 44: "PrintScreen", 22 | 45: "Insert", 23 | 46: "Delete", 24 | 59: ";", 25 | 61: "=", 26 | 91: "Meta", 27 | 92: "Meta", 28 | 106: "*", 29 | 107: "+", 30 | 108: ",", 31 | 109: "-", 32 | 110: ".", 33 | 111: "/", 34 | 144: "NumLock", 35 | 145: "ScrollLock", 36 | 160: "Shift", 37 | 161: "Shift", 38 | 162: "Control", 39 | 163: "Control", 40 | 164: "Alt", 41 | 165: "Alt", 42 | 173: "-", 43 | 186: ";", 44 | 187: "=", 45 | 188: ",", 46 | 189: "-", 47 | 190: ".", 48 | 191: "/", 49 | 192: "`", 50 | 219: "[", 51 | 220: "\\", 52 | 221: "]", 53 | 222: "'" 54 | } 55 | 56 | export var shift = { 57 | 48: ")", 58 | 49: "!", 59 | 50: "@", 60 | 51: "#", 61 | 52: "$", 62 | 53: "%", 63 | 54: "^", 64 | 55: "&", 65 | 56: "*", 66 | 57: "(", 67 | 59: ":", 68 | 61: "+", 69 | 173: "_", 70 | 186: ":", 71 | 187: "+", 72 | 188: "<", 73 | 189: "_", 74 | 190: ">", 75 | 191: "?", 76 | 192: "~", 77 | 219: "{", 78 | 220: "|", 79 | 221: "}", 80 | 222: "\"" 81 | } 82 | 83 | var mac = typeof navigator != "undefined" && /Mac/.test(navigator.platform) 84 | var ie = typeof navigator != "undefined" && /MSIE \d|Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(navigator.userAgent) 85 | 86 | // Fill in the digit keys 87 | for (var i = 0; i < 10; i++) base[48 + i] = base[96 + i] = String(i) 88 | 89 | // The function keys 90 | for (var i = 1; i <= 24; i++) base[i + 111] = "F" + i 91 | 92 | // And the alphabetic keys 93 | for (var i = 65; i <= 90; i++) { 94 | base[i] = String.fromCharCode(i + 32) 95 | shift[i] = String.fromCharCode(i) 96 | } 97 | 98 | // For each code that doesn't have a shift-equivalent, copy the base name 99 | for (var code in base) if (!shift.hasOwnProperty(code)) shift[code] = base[code] 100 | 101 | export function keyName(event) { 102 | // On macOS, keys held with Shift and Cmd don't reflect the effect of Shift in `.key`. 103 | // On IE, shift effect is never included in `.key`. 104 | var ignoreKey = mac && event.metaKey && event.shiftKey && !event.ctrlKey && !event.altKey || 105 | ie && event.shiftKey && event.key && event.key.length == 1 || 106 | event.key == "Unidentified" 107 | var name = (!ignoreKey && event.key) || 108 | (event.shiftKey ? shift : base)[event.keyCode] || 109 | event.key || "Unidentified" 110 | // Edge sometimes produces wrong names (Issue #3) 111 | if (name == "Esc") name = "Escape" 112 | if (name == "Del") name = "Delete" 113 | // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/ 114 | if (name == "Left") name = "ArrowLeft" 115 | if (name == "Up") name = "ArrowUp" 116 | if (name == "Right") name = "ArrowRight" 117 | if (name == "Down") name = "ArrowDown" 118 | return name 119 | } 120 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "w3c-keyname", 3 | "version": "2.2.8", 4 | "description": "Get a KeyboardEvent.key-style string from an event", 5 | "main": "index.cjs", 6 | "type": "module", 7 | "exports": { 8 | "import": "./index.js", 9 | "require": "./index.cjs" 10 | }, 11 | "module": "index.js", 12 | "types": "index.d.ts", 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/marijnh/w3c-keyname.git" 16 | }, 17 | "keywords": [ 18 | "browser", 19 | "key", 20 | "event", 21 | "key code" 22 | ], 23 | "author": "Marijn Haverbeke ", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/marijnh/w3c-keyname/issues" 27 | }, 28 | "homepage": "https://github.com/marijnh/w3c-keyname#readme", 29 | "scripts": { 30 | "build": "rollup -c", 31 | "watch": "rollup -c -w", 32 | "prepare": "npm run build" 33 | }, 34 | "devDependencies": { 35 | "rollup": "^4.29.1" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | input: "index.js", 3 | output: { 4 | file: "index.cjs", 5 | format: "cjs" 6 | } 7 | } 8 | --------------------------------------------------------------------------------