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);