{"id":20073588,"url":"https://github.com/ragingwind/electron-shortcut-loader","last_synced_at":"2025-05-05T21:30:54.578Z","repository":{"id":57221903,"uuid":"44155296","full_name":"ragingwind/electron-shortcut-loader","owner":"ragingwind","description":"Loading predefined shortcuts with events and options","archived":false,"fork":false,"pushed_at":"2015-12-21T19:17:48.000Z","size":19,"stargazers_count":9,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T04:25:24.590Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ragingwind.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-13T06:00:02.000Z","updated_at":"2022-12-31T15:27:06.000Z","dependencies_parsed_at":"2022-08-29T04:10:09.246Z","dependency_job_id":null,"html_url":"https://github.com/ragingwind/electron-shortcut-loader","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragingwind%2Felectron-shortcut-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragingwind%2Felectron-shortcut-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragingwind%2Felectron-shortcut-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ragingwind%2Felectron-shortcut-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ragingwind","download_url":"https://codeload.github.com/ragingwind/electron-shortcut-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252579993,"owners_count":21771248,"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":[],"created_at":"2024-11-13T14:46:53.610Z","updated_at":"2025-05-05T21:30:54.298Z","avatar_url":"https://github.com/ragingwind.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# electron-shortcut-loader\n\n\u003e Loading pre-defined shortcuts with custom events and more options\n\n![](https://cloud.githubusercontent.com/assets/124117/11163454/1605df60-8b14-11e5-99b6-0ba3006528fb.png)\n\n## Install\n\n```\n$ npm install --save electron-shortcut-loader\n```\n\n\n## Usage\n\nShortcutLoader can load pre-defined shortcuts data from a javascript file with `module.exports` in node\n\n```js\n'use strict';\n\nmodule.exports = {\n\t'Command+A': {\n\t\tevent: 'select-all'\n\t},\n\t'Cmd+Alt+I': {\n\t\tevent: 'show-devtool'\n\t},\n\t'CommandOrControl+?': {\n\t\tevent: 'focus'\n\t},\n\t'Command+?': {\n\t\tevent: 'focus'\n\t},\n\t'Command+J': {\n\t\tevent: () =\u003e {\n\t\t\tconsole.log('You can register individual event');\n\t\t}\n\t}\n};\n```\n, or a JSON object in code.\n\n```js\nconst ShortcutLoader = require('ShortcutLoader');\n\nShortcutLoader.load({\n\t'Command+A': {\n\t\tevent: 'select-all'\n\t},\n\t'Cmd+Alt+I': {\n\t\tevent: 'show-devtool'\n\t},\n});\n```\n\nIt supports two types of API, `static` and `instance` methods that allow you to manage shortcuts on `global` and `local`. For global, the shortcut loader manage the one instances dealing with static method and on the other hand, You can manage multiple set of shortcuts depending on your purpose. For example, it could be possible that create each shortcut loader per browser window.\n\nLet see how to use static method on global mode.\n\n```js\nconst ShortcutLoader = require('electron-shortcut-loader');\n\nShortcutLoader.load('./shortcuts', {\n\tautoRegister: false,\n\tcmdOrCtrl: true,\n}, app);\n\n// register shortcuts in manually\napp.on('ready', function () {\n\tshortcuts.register();\n});\n\napp.on('will-quit', function () {\n\tshortcuts.unregister();\n});\n\n// 'shortcut-pressed' is custom event named from Shortcut Loader\napp.on('shortcut-pressed', function (e) {\n\tconsole.log(e.shortcut, e.event, 'key-event has been fired');\n});\n```\n\n, or You can create a instance by hand to create and manage multiple shortcut set.\n\n```js\nconst loader = new ShortcutLoader();\n\n// with common anonymous function\nloader.load('./shortcuts', {\n\tautoRegister: false,\n\tcmdOrCtrl: true,\n}, (e) =\u003e\n\tconsole.log(e.shortcut, e.event, 'key-event has been fired');\n});\n\n// You can use `app`, the instance of EventEmitter, as a event handler.\n// See demo/index.js for further information.\nconst loaderWithApp = new ShortcutLoader();\nloader.load('./shortcuts2', {\n\tautoRegister: true,\n\tcmdOrCtrl: true,\n}, app);\n\n// events, registered with loaderWithApp, will be fired with `shortcut-pressed` event through to `app`\napp.on('shortcut-pressed', function (e) {\n\tconsole.log(e.shortcut, e.event, 'key-event has been fired');\n});\n```\n\n## API\n\n### ShortcutLoader.load, un/register\n\nStatic methods for ShortcutLoader. see below information of APIs for further information.\n\n### ShortcutLoader()\n\nconstructor of ShortcutLoader.\n\n#### load(input, [options], handler)\n\nLoad shortcuts coming from a file or object.\n\n##### input\n\nType: `string` or `object`\n\nA path of shortcuts file or JSON data which has the set of shortcuts as JSON data, will be registered by `register` method. __the path will be resolved with path of current module willing to import shortcuts.__ See more detailed structure of JSON data.\n\n##### options\n\nWill be delivered to each shortcut created by `electron-shortcut`. See further information of [options](https://github.com/ragingwind/electron-shortcut#options).\n\n##### handler\n\nEvent handler, the instance of `EventEmitter` such as `app` or anonymous function. If is not set, `app` is the default event handler and `shortcut-pressed` is the pre-defined name for event handler.\n\n#### register\n\nshortcuts will be registered.\n\n#### unregister\n\nshortcuts will be unregistered.\n\n## Pre-defined shortcut data\n\nIt should consist of accelerator name and event handler. the accelerator name is the key and event handler could be the name of event or anonymous function for an individual event. not allows extra options in a event.\n\n- `event`: event name which will be fired to the instance of `EventEmitter` such as `app` or through to anonymous function. If is not set? will be fired by the name of accelerator\n\n```js\n// predefine shortcuts with events and options in shortcuts.js\n'use strict';\nmodule.exports = {\n\t'Command+A': {\n\t\tevent: 'select-all'\n\t},\n\t'Cmd+Alt+I': {},\n\t'CommandOrControl+?': {\n\t\tevent: e =\u003e {}\n\t}\n};\n```\n\n## Event\n\nWhen a specific shortcut key has been pressed, `event` object pass through to the event handler.  For `EventEmitter` event handler, `shortcut-pressed` is the pre-defined name of the event and you can use the custom event name. If you set anonymous function at event, the `shortcut name` will be used as the name of the event and both of property of `event`, `shortcut` and `event` will be same.\n\n```js\nShortcutLoader.load('Command+A', e =\u003e {\n\tconsole.log(\n\t\te.shortcut, // the name of accelerator\n\t\te.event // the name of accelerator\n\t)\n});\n\napp.on('shortcut-pressed', e =\u003e {\n\tconsole.log(\n\t\te.shortcut, // the name of accelerator\n\t\te.event // the custom name for the event or accelerator\n\t);\n})\n```\n\n## Run demo\n\n```\n$ npm install \u0026\u0026 npm start\n```\n\n## License\n\nMIT © [ragingwind](http://ragingwind.me)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fragingwind%2Felectron-shortcut-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fragingwind%2Felectron-shortcut-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fragingwind%2Felectron-shortcut-loader/lists"}