feat: support global shortcuts

This commit is contained in:
qier222 2021-03-19 14:24:39 +08:00
parent 2a9a53a940
commit 7efe109c83
No known key found for this signature in database
GPG Key ID: 9C85007ED905F14D
2 changed files with 39 additions and 1 deletions

View File

@ -1,5 +1,12 @@
"use strict";
import { app, protocol, BrowserWindow, shell, dialog } from "electron";
import {
app,
protocol,
BrowserWindow,
shell,
dialog,
globalShortcut,
} from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import { startNeteaseMusicApi } from "./electron/services";
import { initIpcMain } from "./electron/ipcMain.js";
@ -7,6 +14,7 @@ import { createMenu } from "./electron/menu";
import { createTray } from "@/electron/tray";
import { createTouchBar } from "./electron/touchBar";
import { createDockMenu } from "./electron/dockMenu";
import { registerGlobalShortcut } from "./electron/globalShortcut";
import { autoUpdater } from "electron-updater";
import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
import express from "express";
@ -214,6 +222,9 @@ class Background {
// create touch bar
this.window.setTouchBar(createTouchBar(this.window));
// register global shortcuts
registerGlobalShortcut(this.window);
});
app.on("activate", () => {
@ -240,6 +251,11 @@ class Background {
app.on("quit", () => {
this.expressApp.close();
});
app.on("will-quit", () => {
// unregister all global shortcuts
globalShortcut.unregisterAll();
});
}
}

View File

@ -0,0 +1,22 @@
const { globalShortcut } = require("electron");
export function registerGlobalShortcut(win) {
globalShortcut.register("Alt+CommandOrControl+P", () => {
win.webContents.send("play");
});
globalShortcut.register("Alt+CommandOrControl+Right", () => {
win.webContents.send("next");
});
globalShortcut.register("Alt+CommandOrControl+Left", () => {
win.webContents.send("previous");
});
globalShortcut.register("Alt+CommandOrControl+Up", () => {
win.webContents.send("increaseVolume");
});
globalShortcut.register("Alt+CommandOrControl+Down", () => {
win.webContents.send("decreaseVolume");
});
globalShortcut.register("Alt+CommandOrControl+L", () => {
win.webContents.send("like");
});
}