electron在mac电脑上复制粘贴全选失效

判断mac环境下手动注册快捷键

注意globalShortcut注册之后会覆盖其他的快捷键,所以blur的时候取消快捷键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { app, BrowserWindow, screen, globalShortcut } from "electron";

let mainWindow;

app.on("browser-window-focus", () => {
if (!mainWindow) return;
if (process.platform === "darwin") {
let contents = mainWindow.webContents;
globalShortcut.register("CommandOrControl+C", () => {
contents.copy();
});
globalShortcut.register("CommandOrControl+V", () => {
contents.paste();
});
globalShortcut.register("CommandOrControl+X", () => {
contents.cut();
});
globalShortcut.register("CommandOrControl+A", () => {
contents.selectAll();
});
}
});

app.on("browser-window-blur", () => {
globalShortcut.unregisterAll();
});

electron文档