From a1286ad0576ad1735e6d16326ee47303c8e73cfa Mon Sep 17 00:00:00 2001 From: oomeow Date: Thu, 18 Dec 2025 22:21:15 +0800 Subject: [PATCH] fix: always occupies hotkey globally when app launch silently (#5866) * fix: always occupies hotkey when app launch silently * docs: update Changelog.md * chore: update --- Changelog.md | 1 + src-tauri/src/core/hotkey.rs | 8 +++-- src-tauri/src/lib.rs | 4 ++- src-tauri/src/utils/resolve/mod.rs | 4 ++- src/main.tsx | 6 ++++ src/utils/parse-hotkey.ts | 57 +++++------------------------- 6 files changed, 28 insertions(+), 52 deletions(-) diff --git a/Changelog.md b/Changelog.md index 46dbeb3a3..46dbd71b3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -29,6 +29,7 @@ - 修复 Windows 下自定义标题栏按钮在最小化 / 关闭后 hover 状态残留 - 修复直接覆盖 `config.yaml` 使用时无法展开代理组 - 修复 macOS 下应用启动时系统托盘图标颜色闪烁 +- 修复应用静默启动模式下非全局热键一直抢占其他应用按键问题
✨ 新增功能 diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index d1ab1e339..be7b927b5 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -287,8 +287,12 @@ singleton!(Hotkey, INSTANCE); impl Hotkey { pub async fn init(&self, skip: bool) -> Result<()> { + if skip { + logging!(debug, Type::Hotkey, "skip register all hotkeys"); + return Ok(()); + } let verge = Config::verge().await; - let enable_global_hotkey = !skip && verge.data_arc().enable_global_hotkey.unwrap_or(true); + let enable_global_hotkey = verge.latest_arc().enable_global_hotkey.unwrap_or(true); logging!( debug, @@ -298,7 +302,7 @@ impl Hotkey { ); // Extract hotkeys data before async operations - let hotkeys = verge.data_arc().hotkeys.clone(); + let hotkeys = verge.latest_arc().hotkeys.clone(); if let Some(hotkeys) = hotkeys { logging!(debug, Type::Hotkey, "Has {} hotkeys need to register", hotkeys.len()); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2bbaed1ad..42d33c9ed 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -336,7 +336,9 @@ pub fn run() { .register_system_hotkey(SystemHotkey::CmdW) .await; } - let _ = hotkey::Hotkey::global().init(true).await; + if !is_enable_global_hotkey { + let _ = hotkey::Hotkey::global().init(false).await; + } return; } diff --git a/src-tauri/src/utils/resolve/mod.rs b/src-tauri/src/utils/resolve/mod.rs index 2d18fff24..042d5d56e 100644 --- a/src-tauri/src/utils/resolve/mod.rs +++ b/src-tauri/src/utils/resolve/mod.rs @@ -131,7 +131,9 @@ pub(super) async fn init_timer() { } pub(super) async fn init_hotkey() { - logging_error!(Type::Setup, Hotkey::global().init(false).await); + // if hotkey is not use by global, skip init it + let skip_register_hotkeys = !Config::verge().await.latest_arc().enable_global_hotkey.unwrap_or(true); + logging_error!(Type::Setup, Hotkey::global().init(skip_register_hotkeys).await); } pub(super) async fn init_auto_lightweight_boot() { diff --git a/src/main.tsx b/src/main.tsx index c2b9d42ca..6680f8812 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -101,3 +101,9 @@ window.addEventListener("beforeunload", () => { // Clean up all WebSocket instances to prevent memory leaks MihomoWebSocket.cleanupAll(); }); + +// Page loaded event +window.addEventListener("DOMContentLoaded", () => { + // Clean up all WebSocket instances to prevent memory leaks + MihomoWebSocket.cleanupAll(); +}); diff --git a/src/utils/parse-hotkey.ts b/src/utils/parse-hotkey.ts index 4335a9724..ddc275d2c 100644 --- a/src/utils/parse-hotkey.ts +++ b/src/utils/parse-hotkey.ts @@ -1,53 +1,9 @@ import { KeyboardEvent } from "react"; -import { debugLog } from "@/utils/debug"; +import getSystem from "./get-system"; -const KEY_MAP: Record = { - // Option + 特殊字符映射 - "–": "Minus", // Option + - - "≠": "Equal", // Option + = - "\u201C": "BracketLeft", // Option + [ - "\u2019": "BracketRight", // Option + ] - "«": "Backslash", // Option + \ - "…": "Semicolon", // Option + ; - æ: "Quote", // Option + ' - "≤": "Comma", // Option + , - "≥": "Period", // Option + . - "÷": "Slash", // Option + / +const OS = getSystem(); - // Option组合键映射 - Å: "A", - "∫": "B", - Ç: "C", - "∂": "D", - "´": "E", - ƒ: "F", - "©": "G", - "˙": "H", - ˆ: "I", - "∆": "J", - "˚": "K", - "¬": "L", - µ: "M", - "˜": "N", - Ø: "O", - π: "P", - Œ: "Q", - "®": "R", - ß: "S", - "†": "T", - "¨": "U", - "√": "V", - "∑": "W", - "≈": "X", - "¥": "Y", - Ω: "Z", -}; - -const mapKeyCombination = (key: string): string => { - const mappedKey = KEY_MAP[key] || key; - return `${mappedKey}`; -}; export const parseHotkey = (keyEvent: KeyboardEvent) => { const nativeEvent = keyEvent.nativeEvent; const key = nativeEvent.code; @@ -64,16 +20,21 @@ export const parseHotkey = (keyEvent: KeyboardEvent) => { } else if (temp.endsWith("RIGHT")) { temp = temp.slice(0, -5); } - debugLog(temp, mapKeyCombination(temp)); switch (temp) { case "CONTROL": return "CTRL"; + case "ALT": + if (OS === "macos") { + return "OPTION"; + } else { + return "ALT"; + } case "META": return "CMD"; case " ": return "SPACE"; default: - return KEY_MAP[temp] || temp; + return temp; } };