From b77cf802691c6b0bb0fa61c75c76ddc6ab42b8b5 Mon Sep 17 00:00:00 2001 From: pentamassiv Date: Sat, 18 Nov 2023 19:45:15 +0100 Subject: [PATCH] All: Allow using unlisted keys --- src/keycodes.rs | 18 ++++++++++++++++-- src/macos/macos_impl.rs | 6 ++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/keycodes.rs b/src/keycodes.rs index dbb8382c..63d9dd0b 100644 --- a/src/keycodes.rs +++ b/src/keycodes.rs @@ -573,6 +573,13 @@ pub enum Key { /// Unicode character #[doc(alias = "Layout")] Unicode(char), + /// Use this for keys that are not listed here that you know the + /// value of. Let us know if you think the key should be listed so + /// we can add it + /// On Linux, this will result in a keysym, + /// On Windows, this will result in a Virtual_Key and + /// On macOS, this will yield a KeyCode + Other(u32), } #[cfg(target_os = "linux")] @@ -674,6 +681,7 @@ impl From for xkeysym::Keysym { Key::VolumeUp => Keysym::XF86_AudioRaiseVolume, Key::VolumeMute => Keysym::XF86_AudioMute, Key::Command | Key::Super | Key::Windows | Key::Meta => Keysym::Super_L, + Key::Other(v) => Keysym::from(v), } } } @@ -686,8 +694,8 @@ impl TryFrom for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY { #[allow(clippy::too_many_lines)] fn try_from(key: Key) -> Result { use windows::Win32::UI::Input::KeyboardAndMouse::{ - VK__none_, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9, VK_A, - VK_ABNT_C1, VK_ABNT_C2, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN, VK_B, VK_BACK, + VK__none_, VIRTUAL_KEY, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9, + VK_A, VK_ABNT_C1, VK_ABNT_C2, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN, VK_B, VK_BACK, VK_BROWSER_BACK, VK_BROWSER_FAVORITES, VK_BROWSER_FORWARD, VK_BROWSER_HOME, VK_BROWSER_REFRESH, VK_BROWSER_SEARCH, VK_BROWSER_STOP, VK_C, VK_CANCEL, VK_CAPITAL, VK_CLEAR, VK_CONTROL, VK_CONVERT, VK_CRSEL, VK_D, VK_DBE_ALPHANUMERIC, @@ -978,6 +986,12 @@ impl TryFrom for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY { Key::XButton2 => VK_XBUTTON2, Key::Zoom => VK_ZOOM, Key::Unicode(_) => return Err("Unicode must be entered via scancodes"), + Key::Other(v) => { + let Ok(v) = u16::try_from(v) else { + return Err("virtual keycodes on Windows have to fit into u16"); + }; + VIRTUAL_KEY(v) + } Key::Super | Key::Command | Key::Windows | Key::Meta | Key::LWin => VK_LWIN, }; diff --git a/src/macos/macos_impl.rs b/src/macos/macos_impl.rs index 493c0fdf..23f616b2 100644 --- a/src/macos/macos_impl.rs +++ b/src/macos/macos_impl.rs @@ -559,6 +559,12 @@ impl From for core_graphics::event::CGKeyCode { Key::VolumeUp => KeyCode::VOLUME_UP, Key::VolumeMute => KeyCode::MUTE, Key::Unicode(c) => get_layoutdependent_keycode(&c.to_string()), + Key::Other(v) => { + let Ok(v) = u16::try_from(v) else { + return Err("virtual keycodes on macOS have to fit into u16"); + }; + KeyCode(v) + } Key::Super | Key::Command | Key::Windows | Key::Meta => KeyCode::COMMAND, } }