Skip to content

Commit

Permalink
All: Allow using unlisted keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pentamassiv committed Nov 18, 2023
1 parent edd7136 commit b77cf80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -674,6 +681,7 @@ impl From<Key> 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),
}
}
}
Expand All @@ -686,8 +694,8 @@ impl TryFrom<Key> for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY {
#[allow(clippy::too_many_lines)]
fn try_from(key: Key) -> Result<Self, Self::Error> {
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,
Expand Down Expand Up @@ -978,6 +986,12 @@ impl TryFrom<Key> 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,
};

Expand Down
6 changes: 6 additions & 0 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,12 @@ impl From<Key> 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,
}
}
Expand Down

0 comments on commit b77cf80

Please sign in to comment.