{"_id":"shortcut-key","_rev":"680313","name":"shortcut-key","description":"library for defining and dispatching keyboard shortcuts","dist-tags":{"latest":"1.0.0"},"maintainers":[{"name":"houzhanfeng","email":"admin@xhou.net"}],"time":{"modified":"2021-08-30T02:34:06.000Z","created":"2017-03-11T09:35:49.833Z","1.0.0":"2017-03-11T09:35:49.833Z"},"users":{"cemaltun":true,"lestad":true},"author":{"name":"Houfeng"},"repository":{"type":"git","url":"git+https://github.com/houfeng/shortcut-key.git"},"versions":{"1.0.0":{"name":"shortcut-key","description":"library for defining and dispatching keyboard shortcuts","version":"1.0.0","author":{"name":"Houfeng"},"repository":{"type":"git","url":"git+https://github.com/houfeng/shortcut-key.git"},"main":"./index.js","gitHead":"dbcff7f5afc9852caec8c553b36c664f7dae93d6","bugs":{"url":"https://github.com/houfeng/shortcut-key/issues"},"homepage":"https://github.com/houfeng/shortcut-key#readme","_id":"shortcut-key@1.0.0","scripts":{},"_shasum":"cb6ce2c759d19f96ec1c822b9389d0adb26923d2","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.6.0","_npmUser":{"name":"houzhanfeng","email":"admin@xhou.net"},"dist":{"shasum":"cb6ce2c759d19f96ec1c822b9389d0adb26923d2","size":15432,"noattachment":false,"key":"/shortcut-key/-/shortcut-key-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/shortcut-key/download/shortcut-key-1.0.0.tgz"},"maintainers":[{"name":"houzhanfeng","email":"admin@xhou.net"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/shortcut-key-1.0.0.tgz_1489224949609_0.7826107733417302"},"directories":{},"publish_time":1489224949833,"_cnpm_publish_time":1489224949833,"_hasShrinkwrap":false}},"readme":"# shortcut-key.js\n\nshortcut-key is a simple micro-library for defining and\ndispatching keyboard shortcuts in web applications.\n\nIt has no dependencies.\n\n*It’s a work in progress (e.g. beta), so spare me your nerdrage and instead\ncontribute! Patches are welcome, but they are not guaranteed to make\nit in.*\n\n## Usage\n\nInclude `shortcut-key.js` in your web app*, by loading it as usual:\n\n```html\n<script src=\"shortcut-key.js\"></script>\n```\n\nshortcut-key has no dependencies and can be used completely standalone.\nIt should not interfere with any JavaScript libraries or frameworks.\n\n_*Preferably use a minified version that fits your workflow. You can\nrun `make` to have UglifyJS (if you have it installed) create a\n`shortcut-key.min.js` file for you._\n\n## Defining shortcuts\n\nOne global method is exposed, `key` which defines shortcuts when\ncalled directly.\n\n```javascript\n// define short of 'a'\nkey('a', function(){ alert('you pressed a!') });\n\n// returning false stops the event and prevents default browser events\nkey('ctrl+r', function(){ alert('stopped reload!'); return false });\n\n// multiple shortcuts that do the same thing\nkey('⌘+r, ctrl+r', function(){ });\n```\n\nThe handler method is called with two arguments set, the keydown `event` fired, and\nan object containing, among others, the following two properties:\n\n`shortcut`: a string that contains the shortcut used, e.g. `ctrl+r`\n`scope`: a string describing the scope (or `all`)\n\n```javascript\nkey('⌘+r, ctrl+r', function(event, handler){\n  console.log(handler.shortcut, handler.scope);\n});\n\n// \"ctrl+r\", \"all\"\n```\n\n\n## Supported keys\n\nshortcut-key understands the following modifiers:\n`⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, and `⌘`.\n\nThe following special keys can be used for shortcuts:\n`backspace`, `tab`, `clear`, `enter`, `return`, `esc`, `escape`, `space`,\n`up`, `down`, `left`, `right`, `home`, `end`, `pageup`, `pagedown`, `del`, `delete`\nand `f1` through `f19`.\n\n\n## Modifier key queries\n\nAt any point in time (even in code other than key shortcut handlers),\nyou can query the `key` object for the state of any keys. This\nallows easy implementation of things like shift+click handlers. For example,\n`key.shift` is `true` if the shift key is currently pressed.\n\n```javascript\nif(key.shift) alert('shift is pressed, OMGZ!');\n```\n\n\n## Other key queries\n\nAt any point in time (even in code other than key shortcut handlers),\nyou can query the `key` object for the state of any key. This\nis very helpful for game development using a game loop. For example,\n`key.isPressed(77)` is `true` if the M key is currently pressed.\n\n```javascript\nif(key.isPressed(\"M\")) alert('M key is pressed, can ya believe it!?');\nif(key.isPressed(77)) alert('M key is pressed, can ya believe it!?');\n```\n\nYou can also get these as an array using...\n```javascript\nkey.getPressedKeyCodes() // returns an array of key codes currently pressed\n```\n\n\n## Scopes\n\nIf you want to reuse the same shortcut for seperate areas in your single page app,\nshortcut-key supports switching between scopes. Use the `key.setScope` method to set scope.\n\n```javascript\n// define shortcuts with a scope\nkey('o, enter', 'issues', function(){ /* do something */ });\nkey('o, enter', 'files', function(){ /* do something else */ });\n\n// set the scope (only 'all' and 'issues' shortcuts will be honored)\nkey.setScope('issues'); // default scope is 'all'\n```\n\n\n## Filter key presses\n\nBy default, when an `INPUT`, `SELECT` or `TEXTAREA` element is focused, shortcut-key doesn't process any shortcuts.\n\nYou can change this by overwriting `key.filter` with a new function. This function is called before\nshortcut-key processes shortcuts, with the keydown event as argument.\n\nIf your function returns false, then the no shortcuts will be processed.\n\nHere's the default implementation for reference:\n\n```javascript\nfunction filter(event){\n  var tagName = (event.target || event.srcElement).tagName;\n  return !(tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');\n}\n```\n\nIf you only want _some_ shortcuts to work while in an input element, you can change the scope in the\n`key.filter` function. Here's an example implementation, setting the scope to either `'input'` or `'other'`.\nDon't forget to return `true` so the any shortcuts get processed.\n\n```javascript\nkey.filter = function(event){\n  var tagName = (event.target || event.srcElement).tagName;\n  key.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other');\n  return true;\n}\n```\n\nHowever a more robust way to handle this is to use proper\nfocus and blur event handlers on your input element, and change scopes there as you see fit.\n\n\n## noConflict mode\n\nYou can call ```key.noConflict``` to remove the ```key``` function from global scope and restore whatever ```key``` was defined to before shortcut-key was loaded. Calling ```key.noConflict``` will return the shortcut-key ```key``` function.\n\n```javascript\nvar k = key.noConflict();\nk('a', function() { /* ... */ });\n\nkey()\n// --> TypeError: 'undefined' is not a function\n```\n\n\n## Unbinding shortcuts\n\nSimilar to defining shortcuts, they can be unbound using `key.unbind`.\n\n```javascript\n// unbind 'a' handler\nkey.unbind('a');\n\n// unbind a key only for a single scope\n// when no scope is specified it defaults to the current scope (key.getScope())\nkey.unbind('o, enter', 'issues');\nkey.unbind('o, enter', 'files');\n```\n\n\n## Notes\n\nshortcut-key should work with any browser that fires `keyup` and `keydown` events,\nand is tested with IE (6+), Safari, Firefox and Chrome.\n\nSee [http://madrobby.github.com/shortcut-key/](http://madrobby.github.com/shortcut-key/) for a live demo.\n\n\n## CoffeeScript\n\nIf you're using CoffeeScript, configuring key shortcuts couldn't be simpler:\n\n```coffeescript\nkey 'a', -> alert('you pressed a!')\n\nkey '⌘+r, ctrl+r', ->\n  alert 'stopped reload!'\n  off\n\nkey 'o, enter', 'issues', ->\n  whatevs()\n\nalert 'shift is pressed, OMGZ!' if key.shift\n```\n\n\n## Contributing\n\nTo contribute, please fork shortcut-key, add your patch and tests for it (in the `test/` folder) and\nsubmit a pull request.\n\n## TODOs\n\n* Finish test suite\n\nshortcut-key is (c) 2011-2013 Thomas Fuchs and may be freely distributed under the MIT license.\nSee the `MIT-LICENSE` file.\n","_attachments":{},"homepage":"https://github.com/houfeng/shortcut-key#readme","bugs":{"url":"https://github.com/houfeng/shortcut-key/issues"}}