electron-localshortcut
Advanced tools
Comparing version 3.1.0 to 3.2.0
71
index.js
@@ -20,3 +20,3 @@ 'use strict'; | ||
return win.getTitle(); | ||
} catch (err) { | ||
} catch { | ||
return 'A destroyed window'; | ||
@@ -33,9 +33,7 @@ } | ||
Error.captureStackTrace(w); | ||
const stack = w.stack ? w.stack.split('\n').slice(4).join('\n') : w.message; | ||
const msg = ` | ||
WARNING: ${accelerator} is not a valid accelerator. | ||
${w.stack | ||
.split('\n') | ||
.slice(4) | ||
.join('\n')} | ||
${stack} | ||
`; | ||
@@ -52,3 +50,2 @@ console.error(msg); | ||
* @param {BrowserWindow} win BrowserWindow instance | ||
* @return {Undefined} | ||
*/ | ||
@@ -69,3 +66,2 @@ function disableAll(win) { | ||
* @param {BrowserWindow} win BrowserWindow instance | ||
* @return {Undefined} | ||
*/ | ||
@@ -87,3 +83,2 @@ function enableAll(win) { | ||
* @param {BrowserWindow} win BrowserWindow instance | ||
* @return {Undefined} | ||
*/ | ||
@@ -94,7 +89,7 @@ function unregisterAll(win) { | ||
const shortcutsOfWindow = windowsWithShortcuts.get(wc); | ||
// Remove listener from window | ||
shortcutsOfWindow.removeListener(); | ||
windowsWithShortcuts.delete(wc); | ||
if (shortcutsOfWindow && shortcutsOfWindow.removeListener) { | ||
// Remove listener from window | ||
shortcutsOfWindow.removeListener(); | ||
windowsWithShortcuts.delete(wc); | ||
} | ||
} | ||
@@ -127,4 +122,6 @@ | ||
} | ||
i++; | ||
} | ||
return -1; | ||
@@ -145,4 +142,6 @@ } | ||
callback(); | ||
return; | ||
} | ||
debug(`eventStamp: ${eventStamp} no match`); | ||
@@ -153,10 +152,9 @@ } | ||
/** | ||
* Registers the shortcut `accelerator`on the BrowserWindow instance. | ||
* Registers the shortcut `accelerator`on the BrowserWindow instance. | ||
* @param {BrowserWindow} win - BrowserWindow instance to register. | ||
* This argument could be omitted, in this case the function register | ||
* the shortcut on all app windows. | ||
* @param {String} accelerator - the shortcut to register | ||
* @param {String|Array<String>} accelerator - the shortcut to register | ||
* @param {Function} callback This function is called when the shortcut is pressed | ||
* and the window is focused and not minimized. | ||
* @return {Undefined} | ||
*/ | ||
@@ -173,2 +171,11 @@ function register(win, accelerator, callback) { | ||
if (Array.isArray(accelerator) === true) { | ||
accelerator.forEach(accelerator => { | ||
if (typeof accelerator === 'string') { | ||
register(win, accelerator, callback); | ||
} | ||
}); | ||
return; | ||
} | ||
debug(`Registering callback for ${accelerator} on window ${title(win)}`); | ||
@@ -181,6 +188,6 @@ _checkAccelerator(accelerator); | ||
if (windowsWithShortcuts.has(wc)) { | ||
debug(`Window has others shortcuts registered.`); | ||
debug('Window has others shortcuts registered.'); | ||
shortcutsOfWindow = windowsWithShortcuts.get(wc); | ||
} else { | ||
debug(`This is the first shortcut of the window.`); | ||
debug('This is the first shortcut of the window.'); | ||
shortcutsOfWindow = []; | ||
@@ -225,3 +232,3 @@ windowsWithShortcuts.set(wc, shortcutsOfWindow); | ||
debug(`Adding shortcut to window set.`); | ||
debug('Adding shortcut to window set.'); | ||
@@ -236,3 +243,3 @@ const eventStamp = toKeyEvent(accelerator); | ||
debug(`Shortcut registered.`); | ||
debug('Shortcut registered.'); | ||
} | ||
@@ -245,4 +252,3 @@ | ||
* on all app windows. If you registered the shortcut on a particular window instance, it will do nothing. | ||
* @param {String} accelerator - the shortcut to unregister | ||
* @return {Undefined} | ||
* @param {String|Array<String>} accelerator - the shortcut to unregister | ||
*/ | ||
@@ -256,8 +262,18 @@ function unregister(win, accelerator) { | ||
if (win.isDestroyed()) { | ||
debug(`Early return because window is destroyed.`); | ||
debug('Early return because window is destroyed.'); | ||
return; | ||
} | ||
wc = win.webContents; | ||
} | ||
if (Array.isArray(accelerator) === true) { | ||
accelerator.forEach(accelerator => { | ||
if (typeof accelerator === 'string') { | ||
unregister(win, accelerator); | ||
} | ||
}); | ||
return; | ||
} | ||
debug(`Unregistering callback for ${accelerator} on window ${title(win)}`); | ||
@@ -270,3 +286,3 @@ | ||
if (!windowsWithShortcuts.has(wc)) { | ||
debug(`Early return because window has never had shortcuts registered.`); | ||
debug('Early return because window has never had shortcuts registered.'); | ||
return; | ||
@@ -292,3 +308,3 @@ } | ||
// Remove window from shrtcuts catalog | ||
// Remove window from shortcuts catalog | ||
windowsWithShortcuts.delete(wc); | ||
@@ -310,2 +326,7 @@ } | ||
_checkAccelerator(accelerator); | ||
const wc = win.webContents; | ||
const shortcutsOfWindow = windowsWithShortcuts.get(wc); | ||
const eventStamp = toKeyEvent(accelerator); | ||
return _findShortcut(eventStamp, shortcutsOfWindow) !== -1; | ||
} | ||
@@ -312,0 +333,0 @@ |
{ | ||
"name": "electron-localshortcut", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "register/unregister a keyboard shortcut locally to a BrowserWindow instance, without using a Menu", | ||
@@ -12,3 +12,3 @@ "repository": "parro-it/electron-localshortcut", | ||
"test": "electron test.js | faucet && xo", | ||
"setup-dev": "npm rebuild --runtime=electron --target=1.6.7 --disturl=https://atom.io/download/atom-shell --abi=51", | ||
"setup-dev": "npm rebuild --runtime=electron --target=4.2.12 --disturl=https://atom.io/download/atom-shell --abi=64", | ||
"tape": "DEBUG=electron-localshortcut electron test.js" | ||
@@ -24,20 +24,21 @@ }, | ||
], | ||
"dependencies": { | ||
"debug": "^4.0.1", | ||
"electron-is-accelerator": "^0.1.0", | ||
"keyboardevent-from-electron-accelerator": "^2.0.0", | ||
"keyboardevents-areequal": "^0.2.1" | ||
}, | ||
"devDependencies": { | ||
"delay": "^2.0.0", | ||
"documentation": "^4.0.0-rc.1", | ||
"electron": "~1.6.7", | ||
"faucet": "^0.0.1", | ||
"ava": "^2.4.0", | ||
"delay": "^4.0.1", | ||
"documentation": "^12.1.3", | ||
"electron": "^4.2.12", | ||
"faucet": "0.0.1", | ||
"p-electron": "^0.8.0", | ||
"p-event": "^1.1.0", | ||
"p-timeout": "^1.0.0", | ||
"robotjs": "^0.4.7", | ||
"p-event": "^4.1.0", | ||
"p-timeout": "^3.2.0", | ||
"robotjs": "^0.5.1", | ||
"tape-async": "^2.3.0", | ||
"xo": "^0.19.0" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.6.8", | ||
"electron-is-accelerator": "^0.1.0", | ||
"keyboardevent-from-electron-accelerator": "^1.1.0", | ||
"keyboardevents-areequal": "^0.2.1" | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -8,4 +8,2 @@ # electron-localshortcut | ||
**Note:** Since this module internally use `global-shortcut` native module, you should not use it until the `ready` event of the app module is emitted. See [electron docs](http://electron.atom.io/docs/latest/api/global-shortcut/). | ||
[![Travis Build Status](https://img.shields.io/travis/parro-it/electron-localshortcut/master.svg)](http://travis-ci.org/parro-it/electron-localshortcut) | ||
@@ -40,2 +38,6 @@ [![NPM module](https://img.shields.io/npm/v/electron-localshortcut.svg)](https://npmjs.org/package/electron-localshortcut) | ||
electronLocalshortcut.register(win, ['Ctrl+R', 'F5'], () => { | ||
console.log('You pressed ctrl & R or F5'); | ||
}); | ||
console.log( | ||
@@ -62,3 +64,2 @@ electronLocalshortcut.isRegistered(win, 'Ctrl+A') | ||
# API | ||
@@ -71,3 +72,5 @@ | ||
Disable all of the shortcuts registered on the BrowserWindow instance. | ||
Registered shortcuts no more works on the `window` instance, but the module keep a reference on them. You can reactivate them later by calling `enableAll` method on the same window instance. | ||
Registered shortcuts no more works on the `window` instance, but the module | ||
keep a reference on them. You can reactivate them later by calling `enableAll` | ||
method on the same window instance. | ||
@@ -78,7 +81,8 @@ **Parameters** | ||
Returns **[Undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
Returns **[Undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
## enableAll | ||
Enable all of the shortcuts registered on the BrowserWindow instance that you had previously disabled calling `disableAll` method. | ||
Enable all of the shortcuts registered on the BrowserWindow instance that | ||
you had previously disabled calling `disableAll` method. | ||
@@ -89,7 +93,9 @@ **Parameters** | ||
Returns **[Undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
Returns **[Undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
## unregisterAll | ||
Unregisters all of the shortcuts registered on any focused BrowserWindow instance. This method does not unregister any shortcut you registered on a particular window instance. | ||
Unregisters all of the shortcuts registered on any focused BrowserWindow | ||
instance. This method does not unregister any shortcut you registered on | ||
a particular window instance. | ||
@@ -100,3 +106,3 @@ **Parameters** | ||
Returns **[Undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
Returns **[Undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
@@ -109,7 +115,10 @@ ## register | ||
- `win` **BrowserWindow** BrowserWindow instance to register. This argument could be omitted, in this case the function register the shortcut on all app windows. | ||
- `accelerator` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the shortcut to register | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** This function is called when the shortcut is pressed and the window is focused and not minimized. | ||
- `win` **BrowserWindow** BrowserWindow instance to register. | ||
This argument could be omitted, in this case the function register | ||
the shortcut on all app windows. | ||
- `accelerator` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)** the shortcut to register | ||
- `callback` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** This function is called when the shortcut is pressed | ||
and the window is focused and not minimized. | ||
Returns **[Undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
Returns **[Undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
@@ -122,18 +131,23 @@ ## unregister | ||
- `win` **BrowserWindow** BrowserWindow instance to unregister. This argument could be omitted, in this case the function unregister the shortcut on all app windows. If you registered the shortcut on a particular window instance, it will do nothing. | ||
- `accelerator` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the shortcut to unregister | ||
- `win` **BrowserWindow** BrowserWindow instance to unregister. | ||
This argument could be omitted, in this case the function unregister the shortcut | ||
on all app windows. If you registered the shortcut on a particular window instance, it will do nothing. | ||
- `accelerator` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)** the shortcut to unregister | ||
Returns **[Undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
Returns **[Undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)** | ||
## isRegistered | ||
Returns `true` or `false` depending on whether the shortcut `accelerator` is | ||
registered on `window`. | ||
Returns `true` or `false` depending on whether the shortcut `accelerator` | ||
is registered on `window`. | ||
**Parameters** | ||
- `win` **BrowserWindow** BrowserWindow instance to check. This argument could be omitted, in this case the function returns whether the shortcut `accelerator` is registered on all app windows. If you registered the shortcut on a particular window instance, it return false. | ||
- `accelerator` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the shortcut to check | ||
- `win` **BrowserWindow** BrowserWindow instance to check. This argument | ||
could be omitted, in this case the function returns whether the shortcut | ||
`accelerator` is registered on all app windows. If you registered the | ||
shortcut on a particular window instance, it return false. | ||
- `accelerator` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the shortcut to check | ||
Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** if the shortcut `accelerator` is registered on `window`. | ||
Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** if the shortcut `accelerator` is registered on `window`. | ||
@@ -140,0 +154,0 @@ # License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17409
269
150
11
+ Addeddebug@4.3.7(transitive)
+ Addedkeyboardevent-from-electron-accelerator@2.0.0(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedkeyboardevent-from-electron-accelerator@1.1.0(transitive)
- Removedms@2.0.0(transitive)
Updateddebug@^4.0.1