index.js
function register(win, accelerator, callback) {
	let wc;
	if (typeof callback === 'undefined') {
		wc = ANY_WINDOW;
		callback = accelerator;
		accelerator = win;
	} else {
		wc = win.webContents;
	}
	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)}`);
	_checkAccelerator(accelerator);

	debug(`${accelerator} seems a valid shortcut sequence.`);

	let shortcutsOfWindow;
	if (windowsWithShortcuts.has(wc)) {
		debug(`Window has others shortcuts registered.`);
		shortcutsOfWindow = windowsWithShortcuts.get(wc);
	} else {
		debug(`This is the first shortcut of the window.`);
		shortcutsOfWindow = [];
		windowsWithShortcuts.set(wc, shortcutsOfWindow);

		if (wc === ANY_WINDOW) {
			const keyHandler = _onBeforeInput(shortcutsOfWindow);
			const enableAppShortcuts = (e, win) => {
				const wc = win.webContents;
				wc.on('before-input-event', keyHandler);
				wc.once('closed', () =>
					wc.removeListener('before-input-event', keyHandler)
				);
			};

			// Enable shortcut on current windows
			const windows = BrowserWindow.getAllWindows();

			windows.forEach(win => enableAppShortcuts(null, win));

			// Enable shortcut on future windows
			app.on('browser-window-created', enableAppShortcuts);

			shortcutsOfWindow.removeListener = () => {
				const windows = BrowserWindow.getAllWindows();
				windows.forEach(win =>
					win.webContents.removeListener('before-input-event', keyHandler)
				);
				app.removeListener('browser-window-created', enableAppShortcuts);
			};
		} else {
			const keyHandler = _onBeforeInput(shortcutsOfWindow);
			wc.on('before-input-event', keyHandler);

			// Save a reference to allow remove of listener from elsewhere
			shortcutsOfWindow.removeListener = () =>
				wc.removeListener('before-input-event', keyHandler);
			wc.once('closed', shortcutsOfWindow.removeListener);
		}
	}

	debug(`Adding shortcut to window set.`);

	const eventStamp = toKeyEvent(accelerator);

	shortcutsOfWindow.push({
		eventStamp,
		callback,
		enabled: true
	});

	debug(`Shortcut registered.`);
}
function register(win, accelerator, callback) {
	let wc;
	if (typeof callback === 'undefined') {
		wc = ANY_WINDOW;
		callback = accelerator;
		accelerator = win;
	} else {
		wc = win.webContents;
	}
	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)}`);
	_checkAccelerator(accelerator);

	debug(`${accelerator} seems a valid shortcut sequence.`);

	let shortcutsOfWindow;
	if (windowsWithShortcuts.has(wc)) {
		debug(`Window has others shortcuts registered.`);
		shortcutsOfWindow = windowsWithShortcuts.get(wc);
	} else {
		debug(`This is the first shortcut of the window.`);
		shortcutsOfWindow = [];
		windowsWithShortcuts.set(wc, shortcutsOfWindow);

		if (wc === ANY_WINDOW) {
			const keyHandler = _onBeforeInput(shortcutsOfWindow);
			const enableAppShortcuts = (e, win) => {
				const wc = win.webContents;
				wc.on('before-input-event', keyHandler);
				wc.once('closed', () =>
					wc.removeListener('before-input-event', keyHandler)
				);
			};

			// Enable shortcut on current windows
			const windows = BrowserWindow.getAllWindows();

			windows.forEach(win => enableAppShortcuts(null, win));

			// Enable shortcut on future windows
			app.on('browser-window-created', enableAppShortcuts);

			shortcutsOfWindow.removeListener = () => {
				const windows = BrowserWindow.getAllWindows();
				windows.forEach(win =>
					win.webContents.removeListener('before-input-event', keyHandler)
				);
				app.removeListener('browser-window-created', enableAppShortcuts);
			};
		} else {
			const keyHandler = _onBeforeInput(shortcutsOfWindow);
			wc.on('before-input-event', keyHandler);

			// Save a reference to allow remove of listener from elsewhere
			shortcutsOfWindow.removeListener = () =>
				wc.removeListener('before-input-event', keyHandler);
			wc.once('closed', shortcutsOfWindow.removeListener);
		}
	}

	debug(`Adding shortcut to window set.`);

	const eventStamp = toKeyEvent(accelerator);

	shortcutsOfWindow.push({
		eventStamp,
		callback,
		enabled: true
	});

	debug(`Shortcut registered.`);
}
function register ( win , accelerator , callback ) { let wc ; if ( typeof callback === 'undefined' ) { wc = ANY_WINDOW ; callback = accelerator ; accelerator = win ; } else { wc = win . webContents ; } if ( Array . isArray ( accelerator ) === true ) { accelerator . forEach ( accelerator => { if ( typeof accelerator === 'string' ) { register ( win , accelerator , callback ) ; } } ) ; return ; } debug ( ` ${ accelerator } ${ title ( win ) } ` ) ; _checkAccelerator ( accelerator ) ; debug ( ` ${ accelerator } ` ) ; let shortcutsOfWindow ; if ( windowsWithShortcuts . has ( wc ) ) { debug ( ` ` ) ; shortcutsOfWindow = windowsWithShortcuts . get ( wc ) ; } else { debug ( ` ` ) ; shortcutsOfWindow = [ ] ; windowsWithShortcuts . set ( wc , shortcutsOfWindow ) ; if ( wc === ANY_WINDOW ) { const keyHandler = _onBeforeInput ( shortcutsOfWindow ) ; const enableAppShortcuts = ( e , win ) => { const wc = win . webContents ; wc . on ( 'before-input-event' , keyHandler ) ; wc . once ( 'closed' , ( ) => wc . removeListener ( 'before-input-event' , keyHandler ) ) ; } ; // Enable shortcut on current windows const windows = BrowserWindow . getAllWindows ( ) ; windows . forEach ( win => enableAppShortcuts ( null , win ) ) ; // Enable shortcut on future windows app . on ( 'browser-window-created' , enableAppShortcuts ) ; shortcutsOfWindow . removeListener = ( ) => { const windows = BrowserWindow . getAllWindows ( ) ; windows . forEach ( win => win . webContents . removeListener ( 'before-input-event' , keyHandler ) ) ; app . removeListener ( 'browser-window-created' , enableAppShortcuts ) ; } ; } else { const keyHandler = _onBeforeInput ( shortcutsOfWindow ) ; wc . on ( 'before-input-event' , keyHandler ) ; // Save a reference to allow remove of listener from elsewhere shortcutsOfWindow . removeListener = ( ) => wc . removeListener ( 'before-input-event' , keyHandler ) ; wc . once ( 'closed' , shortcutsOfWindow . removeListener ) ; } } debug ( ` ` ) ; const eventStamp = toKeyEvent ( accelerator ) ; shortcutsOfWindow . push ( { eventStamp , callback , enabled : true } ) ; debug ( ` ` ) ; }
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|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}
Registers the shortcut accelerator on the BrowserWindow instance .