From f9df226772f6eab31e8fe2db6335b28a3ae350f3 Mon Sep 17 00:00:00 2001 From: impressionyang Date: Wed, 13 May 2026 18:02:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Qt=205.12=20MinGW?= =?UTF-8?q?=20=E4=B8=AD=20win=5Fhotkey.cpp=20=E7=9A=84=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. nativeEventFilter 签名: qintptr* → long* (Qt 5 API) 2. QGuiApplication::instance()->winId() → QGuiApplication::topLevelWindows() (Qt 5 无此方法) 3. GlobalAddAtom → GlobalAddAtomW (MinGW 默认 ANSI 版本) 4. MOD_NOREPEAT 手动定义为 0x4000 (MinGW 7.3.0 SDK 未定义) 5. 添加 #include (避免不完整类型错误) Co-Authored-By: Claude Opus 4.6 --- src/core/win_hotkey.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/core/win_hotkey.cpp b/src/core/win_hotkey.cpp index c10d952..48d9354 100644 --- a/src/core/win_hotkey.cpp +++ b/src/core/win_hotkey.cpp @@ -5,6 +5,14 @@ #include #include #include +#include +#include +#include + +// MOD_NOREPEAT 在 MinGW 7.3.0 的 Windows SDK 中未定义(Windows 8+ 引入) +#ifndef MOD_NOREPEAT +#define MOD_NOREPEAT 0x4000 +#endif #endif static const char* const kTag = "CapsLockVoiceHotkey"; @@ -32,7 +40,7 @@ public: : hotkey_(hotkey) {} bool nativeEventFilter(const QByteArray& eventType, void* message, - qintptr* /*result*/) override { + long* /*result*/) override { if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { auto* msg = static_cast(message); if (msg->message == WM_HOTKEY) { @@ -63,9 +71,12 @@ bool CapsLockVoiceHotkey::start() { if (active_) return true; #ifdef Q_OS_WIN - HWND hwnd = reinterpret_cast(QGuiApplication::instance()->winId()); + HWND hwnd = nullptr; + QWindow* window = QGuiApplication::topLevelWindows().value(0, nullptr); + if (window) { + hwnd = reinterpret_cast(window->winId()); + } if (!hwnd) { - // Try to get the top-level widget's window handle hwnd = GetForegroundWindow(); } @@ -77,7 +88,7 @@ bool CapsLockVoiceHotkey::start() { // 注册 CapsLock (VK_CAPITAL = 0x14) 全局快捷键 // MOD_NOREPEAT 防止按住时重复触发 const int vkCapsLock = 0x14; - impl_->hotkeyId = GlobalAddAtom(L"ImpressVoiceHotkey"); + impl_->hotkeyId = GlobalAddAtomW(L"ImpressVoiceHotkey"); BOOL ok = RegisterHotKey(hwnd, impl_->hotkeyId, MOD_NOREPEAT, vkCapsLock); if (!ok) { @@ -146,7 +157,14 @@ void CapsLockVoiceHotkey::stop() { } // 注销快捷键 - HWND hwnd = reinterpret_cast(QGuiApplication::instance()->winId()); + HWND hwnd = nullptr; + QWindow* stopWindow = QGuiApplication::topLevelWindows().value(0, nullptr); + if (stopWindow) { + hwnd = reinterpret_cast(stopWindow->winId()); + } + if (!hwnd) { + hwnd = GetForegroundWindow(); + } if (hwnd && impl_->hotkeyId) { UnregisterHotKey(hwnd, impl_->hotkeyId); GlobalDeleteAtom(impl_->hotkeyId);