From e90a30c8a4e5b4cc601f0c4b6e85f637d1793bd1 Mon Sep 17 00:00:00 2001 From: lanxiuyun <212650356+lanxiuyun@users.noreply.github.com> Date: Thu, 12 Jun 2025 15:04:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20uIOhook=20=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/common/registerHotKey.ts | 100 +++++++++++++++++------------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/src/main/common/registerHotKey.ts b/src/main/common/registerHotKey.ts index f4b8cc9..22d776f 100644 --- a/src/main/common/registerHotKey.ts +++ b/src/main/common/registerHotKey.ts @@ -58,6 +58,20 @@ const registerHotKey = (mainWindow: BrowserWindow): void => { } }; + function mainWindowPopUp() { + const currentShow = mainWindow.isVisible() && mainWindow.isFocused(); + if (currentShow) return mainWindow.hide(); + const { x: wx, y: wy } = winPosition.getPosition(); + mainWindow.setAlwaysOnTop(false); + mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true }); + mainWindow.focus(); + mainWindow.setVisibleOnAllWorkspaces(false, { + visibleOnFullScreen: true, + }); + mainWindow.setPosition(wx, wy); + mainWindow.show(); + } + const init = async () => { await setAutoLogin(); await setDarkMode(); @@ -67,51 +81,14 @@ const registerHotKey = (mainWindow: BrowserWindow): void => { // 注册偏好快捷键 // 显示/隐藏快捷键 - function mainWindowPopUp() { - const currentShow = mainWindow.isVisible() && mainWindow.isFocused(); - if (currentShow) return mainWindow.hide(); - const { x: wx, y: wy } = winPosition.getPosition(); - mainWindow.setAlwaysOnTop(false); - mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true }); - mainWindow.focus(); - mainWindow.setVisibleOnAllWorkspaces(false, { - visibleOnFullScreen: true, - }); - mainWindow.setPosition(wx, wy); - mainWindow.show(); - } - - let lastModifierPress = Date.now(); if ( - config.perf.shortCut.showAndHidden == 'Ctrl+Ctrl' || - config.perf.shortCut.showAndHidden == 'Option+Option' || - config.perf.shortCut.showAndHidden == 'Shift+Shift' || - config.perf.shortCut.showAndHidden == 'Command+Command' + ['Ctrl+Ctrl', 'Option+Option', 'Shift+Shift', 'Command+Command'].includes( + config.perf.shortCut.showAndHidden + ) ) { - // 双击快捷键,如 Ctrl+Ctrl - uIOhook.stop(); - const modifers = config.perf.shortCut.showAndHidden.split('+'); - const showAndHiddenKey = modifers.pop(); - const key2uioKeyCode = { - Ctrl: UiohookKey.Ctrl, - Shift: UiohookKey.Shift, - Alt: UiohookKey.Alt, - Comma: UiohookKey.Comma, - }; - - uIOhook.on('keydown', (e) => { - if (e.keycode === key2uioKeyCode[showAndHiddenKey]) { - const currentTime = Date.now(); - if (currentTime - lastModifierPress < 300) { - mainWindowPopUp(); - } - lastModifierPress = currentTime; - } - }); - - uIOhook.start(); + // 双击快捷键,详见 uIOhookRegister } else { - // 普通快捷键,如 Ctrl+Space + // 普通快捷键,如 Ctrl+Space,F8 globalShortcut.register(config.perf.shortCut.showAndHidden, () => mainWindowPopUp() ); @@ -141,9 +118,48 @@ const registerHotKey = (mainWindow: BrowserWindow): void => { }); }); }; + + uIOhookRegister(mainWindowPopUp); init(); ipcMain.on('re-register', () => { init(); }); }; export default registerHotKey; + +function uIOhookRegister(callback: () => void) { + let lastModifierPress = Date.now(); + uIOhook.on('keydown', async (uio_event) => { + const config = await localConfig.getConfig(); // 此处还有优化空间 + + if ( + ![ + 'Ctrl+Ctrl', + 'Option+Option', + 'Shift+Shift', + 'Command+Command', + ].includes(config.perf.shortCut.showAndHidden) + ) { + return; + } + + // 双击快捷键,如 Ctrl+Ctrl + const modifers = config.perf.shortCut.showAndHidden.split('+'); + const showAndHiddenKeyStr = modifers.pop(); // Ctrl + const keyStr2uioKeyCode = { + Ctrl: UiohookKey.Ctrl, + Shift: UiohookKey.Shift, + Option: UiohookKey.Alt, + Command: UiohookKey.Comma, + }; + + if (uio_event.keycode === keyStr2uioKeyCode[showAndHiddenKeyStr]) { + const currentTime = Date.now(); + if (currentTime - lastModifierPress < 300) { + callback(); // 调用 mainWindowPopUp + } + lastModifierPress = currentTime; + } + }); + uIOhook.start(); +}