Skip to content

Commit 71bd233

Browse files
committed
use electron-localshortcut to fix #79
1 parent 3c8ba6c commit 71bd233

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

app/appshell/app-menu.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as _ from "lodash";
88
import * as assert from "assert";
99
import { app, Menu } from "electron";
1010
import * as shell from "./shell";
11+
import * as electronLocalshortcut from "electron-localshortcut";
1112

1213
const menuTemplate: MenuItemOptions[] = [];
1314

@@ -20,27 +21,21 @@ app.on("browser-window-blur", function () {
2021
_refreshMenu();
2122
});
2223

23-
// const systemShortcuts = ["Ctrl+Z", "Ctrl+Y", "Ctrl+X", "Ctrl+C", "Ctrl+V", "Ctrl+A"];
24-
25-
function registerShortcuts(menuItem: MenuItemOptions) {
26-
/* this shouldn't be used
24+
function registerShortcuts(win: Electron.BrowserWindow, menuItem: MenuItemOptions) {
2725
if (menuItem.accelerator) {
28-
if (null == systemShortcuts.find((x) => x === menuItem.accelerator)) {
29-
globalShortcut.register(menuItem.accelerator, menuItem.click as Function);
30-
}
26+
electronLocalshortcut.register(win, menuItem.accelerator, menuItem.click as Function);
3127
}
32-
*/
3328
if (Array.isArray(menuItem.submenu)) {
34-
menuItem.submenu.forEach((i) => registerShortcuts(i));
29+
menuItem.submenu.forEach((i) => registerShortcuts(win, i));
3530
}
3631
}
3732

3833
const __refreshMenu = _.debounce(function () {
3934
Menu.setApplicationMenu(Menu.buildFromTemplate(_.cloneDeep(menuTemplate)));
40-
// globalShortcut.unregisterAll();
4135
const mainWindow = shell.getMainWindow();
36+
electronLocalshortcut.unregisterAll(mainWindow);
4237
if (mainWindow.isFocused()) {
43-
menuTemplate.forEach((menuItem) => registerShortcuts(menuItem));
38+
menuTemplate.forEach((menuItem) => registerShortcuts(mainWindow, menuItem));
4439
}
4540
}, 100);
4641

app/electron-localshortcut.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint no-undef:0 */
2+
3+
declare module "electron-localshortcut" {
4+
5+
export function register(window: Electron.BrowserWindow, accelerator: string, callback: Function): void;
6+
7+
export function unregisterAll(window: Electron.BrowserWindow): void;
8+
9+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
}
4747
},
4848
"scripts": {
49+
"dep-change": "grunt dep-change",
4950
"install": "grunt install && npm run build",
5051
"build": "rimraf ./dist && gulp copy-src-dist && grunt build && tsc --project app && tsc --project src",
5152
"build-optimized": "npm run build && grunt optimize",
@@ -63,6 +64,7 @@
6364
"anymatch": "1.3.0",
6465
"chokidar": "1.6.1",
6566
"decompress-zip": "0.3.0",
67+
"electron-localshortcut": "1.1.0",
6668
"fs-extra": "2.0.0",
6769
"isbinaryfile": "3.0.2",
6870
"lodash": "4.17.4",

0 commit comments

Comments
 (0)