优化 uIOhook 逻辑

This commit is contained in:
lanxiuyun 2025-06-12 15:04:10 +08:00
parent 599538db76
commit e90a30c8a4

View File

@ -58,15 +58,6 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
} }
}; };
const init = async () => {
await setAutoLogin();
await setDarkMode();
await setTheme();
const config = await localConfig.getConfig();
globalShortcut.unregisterAll();
// 注册偏好快捷键
// 显示/隐藏快捷键
function mainWindowPopUp() { function mainWindowPopUp() {
const currentShow = mainWindow.isVisible() && mainWindow.isFocused(); const currentShow = mainWindow.isVisible() && mainWindow.isFocused();
if (currentShow) return mainWindow.hide(); if (currentShow) return mainWindow.hide();
@ -81,37 +72,23 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
mainWindow.show(); mainWindow.show();
} }
let lastModifierPress = Date.now(); const init = async () => {
await setAutoLogin();
await setDarkMode();
await setTheme();
const config = await localConfig.getConfig();
globalShortcut.unregisterAll();
// 注册偏好快捷键
// 显示/隐藏快捷键
if ( if (
config.perf.shortCut.showAndHidden == 'Ctrl+Ctrl' || ['Ctrl+Ctrl', 'Option+Option', 'Shift+Shift', 'Command+Command'].includes(
config.perf.shortCut.showAndHidden == 'Option+Option' || config.perf.shortCut.showAndHidden
config.perf.shortCut.showAndHidden == 'Shift+Shift' || )
config.perf.shortCut.showAndHidden == 'Command+Command'
) { ) {
// 双击快捷键,如 Ctrl+Ctrl // 双击快捷键,详见 uIOhookRegister
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();
} else { } else {
// 普通快捷键,如 Ctrl+Space // 普通快捷键,如 Ctrl+SpaceF8
globalShortcut.register(config.perf.shortCut.showAndHidden, () => globalShortcut.register(config.perf.shortCut.showAndHidden, () =>
mainWindowPopUp() mainWindowPopUp()
); );
@ -141,9 +118,48 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
}); });
}); });
}; };
uIOhookRegister(mainWindowPopUp);
init(); init();
ipcMain.on('re-register', () => { ipcMain.on('re-register', () => {
init(); init();
}); });
}; };
export default registerHotKey; 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();
}