From 8ee9d9b0c1d87dae1578ed51918b3f6b2b9d010e Mon Sep 17 00:00:00 2001 From: Exidex <16986685+Exidex@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:59:10 +0100 Subject: [PATCH] Better error messages on windows for registration errors returned by OS --- src/platform_impl/windows/mod.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index db5a6b7..a7718b3 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -6,7 +6,7 @@ use std::ptr; use keyboard_types::{Code, Modifiers}; use windows_sys::Win32::{ - Foundation::{HWND, LPARAM, LRESULT, WPARAM}, + Foundation::{HWND, LPARAM, LRESULT, WPARAM, WIN32_ERROR, ERROR_HOTKEY_ALREADY_REGISTERED}, UI::{ Input::KeyboardAndMouse::*, WindowsAndMessaging::{ @@ -95,7 +95,21 @@ impl GlobalHotKeyManager { let result = unsafe { RegisterHotKey(self.hwnd, hotkey.id() as _, mods, vk_code as _) }; if result == 0 { - return Err(crate::Error::AlreadyRegistered(hotkey)); + let error = std::io::Error::last_os_error(); + + return match error.raw_os_error() { + Some(raw_os_error) => { + let win32error = WIN32_ERROR::try_from(raw_os_error); + if let Ok(ERROR_HOTKEY_ALREADY_REGISTERED) = win32error { + Err(crate::Error::AlreadyRegistered(hotkey)) + } else { + Err(crate::Error::OsError(error)) + } + } + _ => { + Err(crate::Error::OsError(error)) + } + } } } _ => {