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

发布: 2019-12-31 14:18:33标签: 前端开发

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

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

01import { app, BrowserWindow, screen, globalShortcut } from "electron";
02
03let mainWindow;
04
05app.on("browser-window-focus", () => {
06 if (!mainWindow) return;
07 if (process.platform === "darwin") {
08 let contents = mainWindow.webContents;
09 globalShortcut.register("CommandOrControl+C", () => {
10 contents.copy();
11 });
12 globalShortcut.register("CommandOrControl+V", () => {
13 contents.paste();
14 });
15 globalShortcut.register("CommandOrControl+X", () => {
16 contents.cut();
17 });
18 globalShortcut.register("CommandOrControl+A", () => {
19 contents.selectAll();
20 });
21 }
22});
23
24app.on("browser-window-blur", () => {
25 globalShortcut.unregisterAll();
26});
复制代码

electron文档