{"id":25933339,"url":"https://github.com/robertwhurst/keyboardjs","last_synced_at":"2025-05-14T02:08:22.285Z","repository":{"id":1940176,"uuid":"2869424","full_name":"RobertWHurst/KeyboardJS","owner":"RobertWHurst","description":"A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.","archived":false,"fork":false,"pushed_at":"2023-01-19T09:07:23.000Z","size":4563,"stargazers_count":2111,"open_issues_count":12,"forks_count":224,"subscribers_count":41,"default_branch":"master","last_synced_at":"2025-05-11T16:41:10.091Z","etag":null,"topics":["javascript","key-combos","keyboard","keyboard-events","keyboard-hooks","keyboard-shortcuts","keyboardjs","keydown"],"latest_commit_sha":null,"homepage":"http://robertwhurst.github.io/KeyboardJS/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RobertWHurst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["RobertWHurst"]}},"created_at":"2011-11-28T19:07:02.000Z","updated_at":"2025-05-06T16:02:33.000Z","dependencies_parsed_at":"2023-02-10T21:45:31.716Z","dependency_job_id":null,"html_url":"https://github.com/RobertWHurst/KeyboardJS","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertWHurst%2FKeyboardJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertWHurst%2FKeyboardJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertWHurst%2FKeyboardJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertWHurst%2FKeyboardJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobertWHurst","download_url":"https://codeload.github.com/RobertWHurst/KeyboardJS/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254053155,"owners_count":22006717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","key-combos","keyboard","keyboard-events","keyboard-hooks","keyboard-shortcuts","keyboardjs","keydown"],"created_at":"2025-03-04T00:53:29.413Z","updated_at":"2025-05-14T02:08:17.272Z","avatar_url":"https://github.com/RobertWHurst.png","language":"JavaScript","readme":"\nKeyboardJS\n==========\n\n---\n\n\u003e **Note**\n\u003e Please Note: I've create a new libary called\n\u003e [Keystrokes](https://github.com/RobertWHurst/Keystrokes) which serves the same purpose\n\u003e as KeyboardJS, but is more refined, and takes full advantage of modern browsers. If you are\n\u003e considering using KeyboardJS in a new project, I recommend checking out\n\u003e [Keystrokes](https://github.com/RobertWHurst/Keystrokes) first.\n\n---\n\n[![Build Status](http://img.shields.io/github/workflow/status/RobertWHurst/KeyboardJS/Unit%20Tests.svg?style=flat)](https://github.com/RobertWHurst/KeyboardJS/actions)\n[![NPM Version](http://img.shields.io/npm/v/keyboardjs.svg?style=flat)](https://www.npmjs.org/package/keyboardjs)\n[![Downloads This Week](http://img.shields.io/npm/dm/keyboardjs.svg?style=flat)](https://www.npmjs.org/package/keyboardjs)\n[![License](http://img.shields.io/npm/l/keyboardjs.svg?style=flat)](https://www.npmjs.org/package/keyboardjs)\n\nKeyboardJS is a library for use in the browser (node.js compatible). It Allows\ndevelopers to easily setup key bindings. Use key combos to setup complex\nbindings. KeyboardJS also provides contexts. Contexts are great for single page\napplications. They allow you to scope your bindings to various parts of your\napplication. Out of the box keyboardJS uses a US keyboard locale. If you need\nsupport for a different type of keyboard KeyboardJS provides custom locale\nsupport so you can create with a locale that better matches your needs.\n\nKeyboardJS is available as a NPM module. If you're not using a build system\nlike webpack, simply add\n[keyboard.js](https://github.com/RobertWHurst/KeyboardJS/blob/master/dist/keyboard.js)\nor\n[keyboard.min.js](https://github.com/RobertWHurst/KeyboardJS/blob/master/dist/keyboard.min.js)\nfrom the dist folder in this repo to your project via a script tag.\n\n```shell\nnpm install keyboardjs\n```\n\nNote that all key names can be found in [./locales/us.js](https://github.com/RobertWHurst/KeyboardJS/blob/master/locales/us.js).\n\n__Setting up bindings is easy__\n\n```javascript\nkeyboardJS.bind('a', (e) =\u003e {\n  console.log('a is pressed');\n});\nkeyboardJS.bind('a + b', (e) =\u003e {\n  console.log('a and b is pressed');\n});\nkeyboardJS.bind('a + b \u003e c', (e) =\u003e {\n  console.log('a and b then c is pressed');\n});\nkeyboardJS.bind(['a + b \u003e c', 'z + y \u003e z'], (e) =\u003e {\n  console.log('a and b then c or z and y then z is pressed');\n});\nkeyboardJS.bind('', (e) =\u003e {\n  console.log('any key was pressed');\n});\n//alt, shift, ctrl, etc must be lowercase\nkeyboardJS.bind('alt + shift \u003e a', (e) =\u003e {\n  console.log('alt, shift and a is pressed');\n});\n\n// keyboardJS.bind === keyboardJS.on === keyboardJS.addListener\n```\n\n\n__keydown vs a keyup__\n\n```javascript\nkeyboardJS.bind('a', (e) =\u003e {\n  console.log('a is pressed');\n}, (e) =\u003e {\n  console.log('a is released');\n});\nkeyboardJS.bind('a', null, (e) =\u003e {\n  console.log('a is released');\n});\n```\n\n\n__Prevent keydown repeat__\n\n```javascript\nkeyboardJS.bind('a', (e) =\u003e {\n  // this will run once even if a is held\n  e.preventRepeat();\n  console.log('a is pressed');\n});\n```\n\n\n__Unbind things__\n\n```javascript\nkeyboardJS.unbind('a', previouslyBoundHandler);\n// keyboardJS.unbind === keyboardJS.off === keyboardJS.removeListener\n```\n\n\n__Using contexts__\n\n```javascript\n\n  // these will execute in all contexts\n  keyboardJS.bind('a', (e) =\u003e {});\n  keyboardJS.bind('b', (e) =\u003e {});\n  keyboardJS.bind('c', (e) =\u003e {});\n\n  // these will execute in the index context\n  keyboardJS.setContext('index');\n  keyboardJS.bind('1', (e) =\u003e {});\n  keyboardJS.bind('2', (e) =\u003e {});\n  keyboardJS.bind('3', (e) =\u003e {});\n\n  // these will execute in the foo context\n  keyboardJS.setContext('foo');\n  keyboardJS.bind('x', (e) =\u003e {});\n  keyboardJS.bind('y', (e) =\u003e {});\n  keyboardJS.bind('z', (e) =\u003e {});\n\n  // if we have a router we can activate these contexts when appropriate\n  myRouter.on('/', (e) =\u003e {\n    keyboardJS.setContext('index');\n  });\n  myRouter.on('/foo', (e) =\u003e {\n    keyboardJS.setContext('foo');\n  });\n\n  // you can always figure out your context too\n  const contextName = keyboardJS.getContext();\n\n  // you can also set up handlers for a context without losing the current context\n  keyboardJS.withContext('bar', ()  =\u003e{\n    // these will execute in the bar context\n    keyboardJS.bind('7', (e) =\u003e {});\n    keyboardJS.bind('8', (e) =\u003e {});\n    keyboardJS.bind('9', (e) =\u003e {});\n  });\n```\n\n\n__pause, resume, and reset__\n\n```javascript\n\n// the keyboard will no longer trigger bindings\nkeyboardJS.pause();\n\n// the keyboard will once again trigger bindings\nkeyboardJS.resume();\n\n// all active bindings will released and unbound,\n// pressed keys will be cleared\nkeyboardJS.reset();\n```\n\n\n__pressKey, releaseKey, and releaseAllKeys__\n\n```javascript\n\n// pressKey\nkeyboardJS.pressKey('a');\n// or\nkeyboardJS.pressKey(65);\n\n// releaseKey\nkeyboardJS.releaseKey('a');\n// or\nkeyboardJS.releaseKey(65);\n\n// releaseAllKeys\nkeyboardJS.releaseAllKeys();\n```\n\n\n__watch and stop__\n\n```javascript\n// bind to the window and document in the current window\nkeyboardJS.watch();\n\n// or pass your own window and document\nkeyboardJS.watch(myDoc);\nkeyboardJS.watch(myWin, myDoc);\n\n// or scope to a specific element\nkeyboardJS.watch(myForm);\nkeyboardJS.watch(myWin, myForm);\n\n// detach KeyboardJS from the window and document/element\nkeyboardJS.stop();\n```\n","funding_links":["https://github.com/sponsors/RobertWHurst"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertwhurst%2Fkeyboardjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobertwhurst%2Fkeyboardjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertwhurst%2Fkeyboardjs/lists"}