├── .gitignore ├── LICENSE ├── README.md ├── index.ts ├── package.json ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Endel Dreyer 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @gamestdio/keycode 2 | 3 | Use human key names instead of keycode numbers for your keyboard bindings. 4 | 5 | **1.7kb minified** 6 | 7 | ## Warning 8 | 9 | The [`which`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which) and [`keyCode`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) properties are in the process of being deprecated by the browsers. 10 | 11 | After mainstream adoption, you would rather use the new [`key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) or [`code`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code) attributes in `KeyboardEvent`. 12 | 13 | By the time of this writing, IE/Edge still doesn't support the new `KeyboardEvent.key` / `KeyboardEvent.code` API. See: https://caniuse.com/#feat=keyboardevent-code 14 | 15 | ## Usage example 16 | 17 | ```typescript 18 | import * as Keycode from "@gamestdio/keycode"; 19 | 20 | document.addEventListener('keyup', function(e) { 21 | if (e.which == Keycode.ENTER) { 22 | console.log("User pressed ENTER key") 23 | } 24 | }) 25 | ``` 26 | 27 | ## License 28 | 29 | MIT 30 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | export const BACKSPACE = 8; 2 | export const TAB = 9; 3 | export const ENTER = 13; 4 | export const SHIFT = 16; 5 | export const CTRL = 17; 6 | export const ALT = 18; 7 | export const PAUSE = 19; 8 | export const CAPSLOCK = 20; 9 | export const ESCAPE = 27; 10 | export const SPACE = 32; 11 | 12 | export const PAGEUP = 33; 13 | export const PAGEDOWN = 34; 14 | export const END = 35; 15 | export const HOME = 36; 16 | export const LEFT = 37; 17 | export const UP = 38; 18 | export const RIGHT = 39; 19 | export const DOWN = 40; 20 | export const INSERT = 45; 21 | export const DELETE = 46; 22 | 23 | // numbers 24 | export const KEY_0 = 48; 25 | export const KEY_1 = 49; 26 | export const KEY_2 = 50; 27 | export const KEY_3 = 51; 28 | export const KEY_4 = 52; 29 | export const KEY_5 = 53; 30 | export const KEY_6 = 54; 31 | export const KEY_7 = 55; 32 | export const KEY_8 = 56; 33 | export const KEY_9 = 57; 34 | 35 | // alphabet 36 | export const A = 65; 37 | export const B = 66; 38 | export const C = 67; 39 | export const D = 68; 40 | export const E = 69; 41 | export const F = 70; 42 | export const G = 71; 43 | export const H = 72; 44 | export const I = 73; 45 | export const J = 74; 46 | export const K = 75; 47 | export const L = 76; 48 | export const M = 77; 49 | export const N = 78; 50 | export const O = 79; 51 | export const P = 80; 52 | export const Q = 81; 53 | export const R = 82; 54 | export const S = 83; 55 | export const T = 84; 56 | export const U = 85; 57 | export const V = 86; 58 | export const W = 87; 59 | export const X = 88; 60 | export const Y = 89; 61 | export const Z = 90; 62 | 63 | export const SELECT = 93; 64 | 65 | export const NUMPAD_0 = 96; 66 | export const NUMPAD_1 = 97; 67 | export const NUMPAD_2 = 98; 68 | export const NUMPAD_3 = 99; 69 | export const NUMPAD_4 = 100; 70 | export const NUMPAD_5 = 101; 71 | export const NUMPAD_6 = 102; 72 | export const NUMPAD_7 = 103; 73 | export const NUMPAD_8 = 104; 74 | export const NUMPAD_9 = 105; 75 | 76 | export const MULTIPLY = 106; 77 | export const ADD = 107; 78 | export const SUBTRACT = 109; 79 | export const DECIMALPOINT = 110; 80 | export const DIVIDE = 111; 81 | 82 | // F1~F2 83 | export const F1 = 112; 84 | export const F2 = 113; 85 | export const F3 = 114; 86 | export const F4 = 115; 87 | export const F5 = 116; 88 | export const F6 = 117; 89 | export const F7 = 118; 90 | export const F8 = 119; 91 | export const F9 = 120; 92 | export const F10 = 121; 93 | export const F11 = 122; 94 | export const F12 = 123; 95 | 96 | // etc / accents 97 | export const NUMLOCK = 144; 98 | export const SCROLLLOCK = 145; 99 | export const SEMICOLON = 186; 100 | export const EQUALSIGN = 187; 101 | export const COMMA = 188; 102 | export const DASH = 189; 103 | export const PERIOD = 190; 104 | export const FORWARDSLASH = 191; 105 | export const GRAVEACCENT = 192; 106 | export const OPENBRACKET = 219; 107 | export const BACKSLASH = 220; 108 | export const CLOSEBRAKET = 221; 109 | export const SINGLEQUOTE = 222; 110 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@gamestdio/keycode", 3 | "version": "2.0.0", 4 | "description": "Use human key names instead of keycode numbers for your keyboard bindings.", 5 | "author": "Endel Dreyer ", 6 | "keywords": [ 7 | "keyboard", 8 | "keycode", 9 | "keycodes" 10 | ], 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/gamestdio/keycode.js.git" 14 | }, 15 | "main": "index.js", 16 | "scripts": { 17 | "prepublish": "tsc -d" 18 | }, 19 | "license": "MIT", 20 | "devDependencies": { 21 | "typescript": "^1.8.10" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "baseDir": ".", 3 | "compilerOptions": { 4 | "target": "ES5", 5 | "module": "commonjs", 6 | "noImplicitAny": true, 7 | "sourceMap": false, 8 | "allowSyntheticDefaultImports": true 9 | }, 10 | "exclude": [ 11 | "node_modules", 12 | "test" 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | typescript@^2.1.5: 6 | version "2.1.5" 7 | resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.5.tgz#6fe9479e00e01855247cea216e7561bafcdbcd4a" 8 | --------------------------------------------------------------------------------