Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Implement global shortcuts #66

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict'

const electron = require('electron')
const {app, BrowserWindow, ipcMain, Menu, MenuItem, Tray, dialog, Notification} = electron
const {app, BrowserWindow, ipcMain, Menu, MenuItem, Tray, dialog, Notification, globalShortcut} = electron
const consts = require('./src/consts.js')
const client = require('./src/client.js')
const readline = require('readline')
Expand Down Expand Up @@ -349,6 +349,25 @@ function onReady () {
case consts.eventNames.windowCmdWebContentsExecuteJavascript:
elements[json.targetID].webContents.executeJavaScript(json.code).then(() => client.write(json.targetID, consts.eventNames.windowEventWebContentsExecutedJavaScript));
break;

// Global Shortcut
case consts.eventNames.globalShortcutsCmdRegister:
const isRegistered = globalShortcut.register(json.globalShortcuts.accelerator, () => {
client.write(json.targetID, consts.eventNames.globalShortcutsEventTriggered, {globalShortcuts:{accelerator: json.globalShortcuts.accelerator}});
});
client.write(json.targetID, consts.eventNames.globalShortcutsEventRegistered, {globalShortcuts:{isRegistered: isRegistered}});
break;
case consts.eventNames.globalShortcutsCmdIsRegistered:
client.write(json.targetID, consts.eventNames.globalShortcutsEventIsRegistered, {globalShortcuts:{isRegistered: globalShortcut.isRegistered(json.globalShortcuts.accelerator)}});
break;
case consts.eventNames.globalShortcutsCmdUnregister:
globalShortcut.unregister(json.globalShortcuts.accelerator);
client.write(json.targetID, consts.eventNames.globalShortcutsEventUnregistered);
break
case consts.eventNames.globalShortcutsCmdUnregisterAll:
globalShortcut.unregisterAll();
client.write(json.targetID, consts.eventNames.globalShortcutsEventUnregisteredAll);
break
}
});

Expand Down
9 changes: 9 additions & 0 deletions src/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ module.exports = {
dockEventHidden: "dock.event.hidden",
dockEventIconSet: "dock.event.icon.set",
dockEventShown: "dock.event.shown",
globalShortcutsCmdRegister: "global.shortcuts.cmd.register",
globalShortcutsCmdIsRegistered: "global.shortcuts.cmd.is.registered",
globalShortcutsCmdUnregister: "global.shortcuts.cmd.unregister",
globalShortcutsCmdUnregisterAll: "global.shortcuts.cmd.unregister.all",
globalShortcutsEventRegistered: "global.shortcuts.event.registered",
globalShortcutsEventIsRegistered: "global.shortcuts.event.is.registered",
globalShortcutsEventUnregistered: "global.shortcuts.event.unregistered",
globalShortcutsEventUnregisteredAll: "global.shortcuts.event.unregistered.all",
globalShortcutsEventTriggered: "global.shortcuts.event.triggered",
ipcCmdLog: "ipc.cmd.log",
ipcCmdMessage: "ipc.cmd.message",
ipcCmdMessageCallback: "ipc.cmd.message.callback",
Expand Down