Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

electron-localshortcut

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electron-localshortcut - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

README.md

245

index.js
'use strict';
const electron = require('electron');
const {globalShortcut, BrowserWindow, app} = require('electron');
const isAccelerator = require('electron-is-accelerator');
const _debug = require('debug');
const globalShortcut = electron.globalShortcut;
const BrowserWindow = electron.BrowserWindow;
const app = electron.app;
const debug = _debug('electron-localshortcut');
const windowsWithShortcuts = new WeakMap();

@@ -14,25 +13,20 @@

function unregisterAllShortcuts(win) {
const shortcuts = windowsWithShortcuts.get(win);
if (shortcuts) {
shortcuts.forEach(sc =>
globalShortcut.unregister(sc.accelerator)
);
}
}
let _enableShortcut = shortcut => {
globalShortcut.register(shortcut.accelerator, shortcut.callback);
shortcut.registered = true;
};
function registerAllShortcuts(win) {
const shortcuts = windowsWithShortcuts.get(win);
if (shortcuts) {
shortcuts.forEach(sc =>
globalShortcut.register(sc.accelerator, sc.callback)
);
}
let _disableShortcut = shortcut => {
globalShortcut.unregister(shortcut.accelerator);
shortcut.registered = false;
};
function __mockup(enableShortcut, disableShortcut) {
_enableShortcut = enableShortcut;
_disableShortcut = disableShortcut;
}
function unregisterAll(win) {
if (win === undefined) {
// Unregister shortcuts for any window in the app
unregisterAll(ANY_WINDOW);
return;
function _enableWindowAndApp(win) {
if (windowsWithShortcuts.has(ANY_WINDOW)) {
enableAll(ANY_WINDOW);
}

@@ -44,43 +38,22 @@

unregisterAllShortcuts(win);
windowsWithShortcuts.delete(win);
enableAll(win);
}
function register(win, accelerator, callback) {
if (arguments.length === 2 && typeof win === 'string') {
// Register shortcut for any window in the app
// win = accelerator, accelerator = callback
register(ANY_WINDOW, win, accelerator);
return;
function _disableWindowAndApp(win) {
if (windowsWithShortcuts.has(ANY_WINDOW)) {
disableAll(ANY_WINDOW);
}
checkAccelerator(accelerator);
if (windowsWithShortcuts.has(win)) {
const shortcuts = windowsWithShortcuts.get(win);
shortcuts.push({
accelerator,
callback
});
} else {
windowsWithShortcuts.set(win, [{
accelerator,
callback
}]);
win.on('close', () => {
unregisterAllShortcuts(win);
});
if (!windowsWithShortcuts.has(win)) {
return;
}
const focusedWin = BrowserWindow.getFocusedWindow();
if ((win === ANY_WINDOW && focusedWin !== null) || focusedWin === win) {
globalShortcut.register(accelerator, callback);
}
disableAll(win);
}
function indexOfShortcut(win, accelerator) {
function _indexOfShortcut(win, accelerator) {
if (!windowsWithShortcuts.has(win)) {
return -1;
}
checkAccelerator(accelerator);
_checkAccelerator(accelerator);

@@ -99,3 +72,3 @@ const shortcuts = windowsWithShortcuts.get(win);

function checkAccelerator(accelerator) {
function _checkAccelerator(accelerator) {
if (!isAccelerator(accelerator)) {

@@ -113,2 +86,113 @@ const w = {};

/**
* 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.
* @param {BrowserWindow} win BrowserWindow instance
* @return {Undefined}
*/
function disableAll(win) {
const shortcuts = windowsWithShortcuts.get(win);
if (shortcuts) {
shortcuts.forEach(_disableShortcut);
}
}
/**
* Enable all of the shortcuts registered on the BrowserWindow instance that you had previously disabled calling `disableAll` method.
* @param {BrowserWindow} win BrowserWindow instance
* @return {Undefined}
*/
function enableAll(win) {
const shortcuts = windowsWithShortcuts.get(win);
if (shortcuts) {
shortcuts.forEach(_enableShortcut);
}
}
/**
* 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.
* @param {BrowserWindow} win BrowserWindow instance
* @return {Undefined}
*/
function unregisterAll(win) {
if (win === undefined) {
// Unregister shortcuts for any window in the app
unregisterAll(ANY_WINDOW);
return;
}
if (!windowsWithShortcuts.has(win)) {
return;
}
disableAll(win);
windowsWithShortcuts.delete(win);
}
/**
* 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 {Function} callback This function is called when the shortcut is pressed and the window is focused and not minimized.
* @return {Undefined}
*/
function register(win, accelerator, callback) {
if (arguments.length === 2 && typeof win === 'string') {
// Register shortcut for any window in the app
// win = accelerator, accelerator = callback
register(ANY_WINDOW, win, accelerator);
return;
}
_checkAccelerator(accelerator);
const newShortcut = {accelerator, callback, registered: false};
const _unregister = () => {
_disableWindowAndApp(win);
};
const _register = () => {
_enableWindowAndApp(win);
};
if (windowsWithShortcuts.has(win)) {
const shortcuts = windowsWithShortcuts.get(win);
shortcuts.push(newShortcut);
} else {
windowsWithShortcuts.set(win, [newShortcut]);
if (win !== ANY_WINDOW) {
win.on('close', _unregister);
win.on('hide', _unregister);
win.on('minimize', _unregister);
win.on('restore', _register);
win.on('show', _register);
}
}
const focusedWin = BrowserWindow.getFocusedWindow();
const registeringAppShortcut = win === ANY_WINDOW;
const appHasFocus = focusedWin !== null && focusedWin.isVisible();
const registeringWindowHasFocus = focusedWin === win;
const registeringWindowIsMinimized = () => focusedWin.isMinimized();
debug(registeringWindowHasFocus, win && win === ANY_WINDOW ? 'ANY_WINDOW' : win.getTitle(), focusedWin && focusedWin.getTitle());
debug(JSON.stringify({registeringAppShortcut, appHasFocus, registeringWindowHasFocus}));
if ((registeringAppShortcut && appHasFocus) ||
(registeringWindowHasFocus && !registeringWindowIsMinimized())) {
_register();
}
}
/**
* Unregisters the shortcut of `accelerator` registered on the BrowserWindow instance.
* @param {BrowserWindow} win - 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.
* @param {String} accelerator - the shortcut to unregister
* @return {Undefined}
*/
function unregister(win, accelerator) {

@@ -122,8 +206,8 @@ if (arguments.length === 1 && typeof win === 'string') {

checkAccelerator(accelerator);
_checkAccelerator(accelerator);
const shortcutToUnregisterIdx = indexOfShortcut(win, accelerator);
const shortcutToUnregisterIdx = _indexOfShortcut(win, accelerator);
if (shortcutToUnregisterIdx !== -1) {
globalShortcut.unregister(accelerator);
_disableShortcut(accelerator);
const shortcuts = windowsWithShortcuts.get(win);

@@ -134,2 +218,9 @@ shortcuts.splice(shortcutToUnregisterIdx, 1);

/**
* Returns `true` or `false` depending on whether the shortcut `accelerator` is
registered on `window`.
* @param {BrowserWindow} win - 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.
* @param {String} accelerator - the shortcut to check
* @return {Boolean} - if the shortcut `accelerator` is registered on `window`.
*/
function isRegistered(win, accelerator) {

@@ -142,36 +233,13 @@ if (arguments.length === 1 && typeof win === 'string') {

checkAccelerator(accelerator);
_checkAccelerator(accelerator);
return indexOfShortcut(win, accelerator) !== -1;
return _indexOfShortcut(win, accelerator) !== -1;
}
app.on('browser-window-focus', (e, win) => {
if (windowsWithShortcuts.has(ANY_WINDOW)) {
registerAllShortcuts(ANY_WINDOW);
}
app.on('browser-window-focus', (_, win) => _enableWindowAndApp(win));
app.on('browser-window-blur', (_, win) => _disableWindowAndApp(win));
if (!windowsWithShortcuts.has(win)) {
return;
}
registerAllShortcuts(win);
});
app.on('browser-window-blur', (e, win) => {
if (windowsWithShortcuts.has(ANY_WINDOW)) {
unregisterAllShortcuts(ANY_WINDOW);
}
if (!windowsWithShortcuts.has(win)) {
return;
}
unregisterAllShortcuts(win);
});
// All shortcuts should be unregistered by closing the window.
// just for double check
app.on('window-all-closed', () => {
unregisterAll();
});
app.on('window-all-closed', unregisterAll);

@@ -183,4 +251,5 @@ module.exports = {

unregisterAll,
enableAll: registerAllShortcuts,
disableAll: unregisterAllShortcuts
enableAll,
disableAll,
__mockup
};
{
"name": "electron-localshortcut",
"version": "1.1.1",
"version": "2.0.0",
"description": "register/unregister a keyboard shortcut locally to a BrowserWindow instance, without using a Menu",

@@ -9,4 +9,7 @@ "repository": "parro-it/electron-localshortcut",

"scripts": {
"start": "electron test.js",
"test": "xo"
"doc": "documentation readme index.js --section=API",
"start": "electron example.js",
"test-local": "electron test.js | faucet && xo",
"test": "xo",
"tape": "DEBUG=electron-localshortcut electron test.js"
},

@@ -22,4 +25,12 @@ "keywords": [

"devDependencies": {
"electron": "^1.4.4",
"xo": "^0.18.0"
"debug": "^2.6.6",
"delay": "^2.0.0",
"documentation": "^4.0.0-rc.1",
"electron": "^1.6.7",
"faucet": "^0.0.1",
"p-electron": "^0.7.0",
"p-event": "^1.1.0",
"p-timeout": "^1.0.0",
"tape-async": "^2.3.0",
"xo": "^0.19.0"
},

@@ -26,0 +37,0 @@ "dependencies": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc