Remove duped code
This commit is contained in:
parent
d4984e3d68
commit
dc96f1c70e
1 changed files with 0 additions and 86 deletions
|
@ -399,92 +399,6 @@ app.on('before-quit', () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Counter for keybindings we have registered
|
|
||||||
let ioHookTasks = 0;
|
|
||||||
|
|
||||||
// Limit for amount of keybindings that can be
|
|
||||||
// registered at once.
|
|
||||||
const keybindingRegistrationLimit = 1;
|
|
||||||
|
|
||||||
// Fires when a global keybinding is being registered
|
|
||||||
ipcMain.on('register-keybinding', function(ev, keybinding) {
|
|
||||||
// Prevent registering more than the defined limit
|
|
||||||
if (ioHookTasks >= keybindingRegistrationLimit) {
|
|
||||||
ioHookTasks = keybindingRegistrationLimit;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start listening for global keyboard shortcuts
|
|
||||||
if (ioHookTasks <= 0) {
|
|
||||||
ioHookTasks = 0;
|
|
||||||
ioHook.start();
|
|
||||||
}
|
|
||||||
ioHookTasks++;
|
|
||||||
|
|
||||||
ioHook.registerShortcut(keybinding.code, () => {
|
|
||||||
ev.sender.send('keybinding-pressed', keybinding.name);
|
|
||||||
}, () => {
|
|
||||||
ev.sender.send('keybinding-released', keybinding.name);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fires when a global keybinding is being unregistered
|
|
||||||
ipcMain.on('unregister-keybinding', function(ev, keybindingCode) {
|
|
||||||
// Stop listening for global keyboard shortcuts if we're
|
|
||||||
// unregistering the last one
|
|
||||||
if (ioHookTasks <= 1) {
|
|
||||||
ioHook.stop();
|
|
||||||
}
|
|
||||||
ioHookTasks--;
|
|
||||||
|
|
||||||
ioHook.unregisterShortcutByKeys(keybindingCode);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Tell renderer process what key was pressed
|
|
||||||
// iohook has its own encoding for keys, so we can't just use a
|
|
||||||
// listener in the renderer process to register iohook shortcuts
|
|
||||||
let renderProcessID = null;
|
|
||||||
const reportKeyEvent = function(keyEvent) {
|
|
||||||
// "this" is the renderer process because we call this method with .bind()
|
|
||||||
renderProcessID.sender.send('keypress', {
|
|
||||||
keydown: keyEvent.type == 'keydown',
|
|
||||||
keycode: keyEvent.keycode,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Fires when listening on all keys
|
|
||||||
// !!Security note: Ensure iohook is only allowed to listen to keybindings
|
|
||||||
// when the browser window is in focus, else an XSS could lead to keylogging
|
|
||||||
// Currently, this is achieved by leveraging browserWindow to act on focus loss
|
|
||||||
ipcMain.on('start-listening-keys', function(ev, keybindingCode) {
|
|
||||||
// Start recording keypresses
|
|
||||||
if (ioHookTasks <= 0) {
|
|
||||||
ioHookTasks = 0;
|
|
||||||
ioHook.start();
|
|
||||||
}
|
|
||||||
ioHookTasks++;
|
|
||||||
|
|
||||||
renderProcessID = ev;
|
|
||||||
ioHook.on('keydown', reportKeyEvent);
|
|
||||||
ioHook.on('keyup', reportKeyEvent);
|
|
||||||
});
|
|
||||||
|
|
||||||
const stopListeningKeys = () => {
|
|
||||||
// Stop recording keypresses
|
|
||||||
ioHook.off('keydown', reportKeyEvent);
|
|
||||||
ioHook.off('keyup', reportKeyEvent);
|
|
||||||
};
|
|
||||||
|
|
||||||
ipcMain.on('stop-listening-keys', () => {
|
|
||||||
if (ioHookTasks <= 1) {
|
|
||||||
ioHookTasks = 1;
|
|
||||||
ioHook.stop();
|
|
||||||
}
|
|
||||||
ioHookTasks--;
|
|
||||||
|
|
||||||
stopListeningKeys();
|
|
||||||
});
|
|
||||||
|
|
||||||
app.on('second-instance', (ev, commandLine, workingDirectory) => {
|
app.on('second-instance', (ev, commandLine, workingDirectory) => {
|
||||||
// If other instance launched with --hidden then skip showing window
|
// If other instance launched with --hidden then skip showing window
|
||||||
if (commandLine.includes('--hidden')) return;
|
if (commandLine.includes('--hidden')) return;
|
||||||
|
|
Loading…
Add table
Reference in a new issue