Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux/macos: Add support for numpad keys #384

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
## Added
- macOS: Add to support Mouse special key(Back, Forward)
- win: Helper function to tell Windows to respect the users scaling settings `set_dpi_awareness`. Read the docs before using it
- linux: Add support for numpad keys (e.g. `Key::Numpad2`)
- macOS: Add support for numpad keys (e.g. `Key::Numpad2`)

## Removed

Expand Down
30 changes: 15 additions & 15 deletions src/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub enum Key {
AbntC2,
#[cfg(target_os = "windows")]
Accept,
#[cfg(target_os = "windows")]
Add,
/// alt key on Linux and Windows (option key on macOS)
Alt,
Expand Down Expand Up @@ -175,11 +174,9 @@ pub enum Key {
DBESBCSChar,
#[cfg(target_os = "windows")]
DBESChar,
#[cfg(target_os = "windows")]
Decimal,
/// delete key
Delete,
#[cfg(target_os = "windows")]
Divide,
/// down arrow key
DownArrow,
Expand Down Expand Up @@ -399,7 +396,6 @@ pub enum Key {
MissionControl,
#[cfg(any(target_os = "windows", all(unix, not(target_os = "macos"))))]
ModeChange,
#[cfg(target_os = "windows")]
Multiply,
#[cfg(target_os = "windows")]
NavigationAccept,
Expand All @@ -425,25 +421,15 @@ pub enum Key {
None,
#[cfg(any(target_os = "windows", all(unix, not(target_os = "macos"))))]
Numlock,
#[cfg(target_os = "windows")]
Numpad0,
#[cfg(target_os = "windows")]
Numpad1,
#[cfg(target_os = "windows")]
Numpad2,
#[cfg(target_os = "windows")]
Numpad3,
#[cfg(target_os = "windows")]
Numpad4,
#[cfg(target_os = "windows")]
Numpad5,
#[cfg(target_os = "windows")]
Numpad6,
#[cfg(target_os = "windows")]
Numpad7,
#[cfg(target_os = "windows")]
Numpad8,
#[cfg(target_os = "windows")]
Numpad9,
#[cfg(target_os = "windows")]
OEM1,
Expand Down Expand Up @@ -579,7 +565,6 @@ pub enum Key {
Snapshot,
/// space key
Space,
#[cfg(target_os = "windows")]
Subtract,
#[deprecated(since = "0.0.12", note = "now renamed to Meta")]
/// super key on linux (command key on macOS, windows key on Windows)
Expand Down Expand Up @@ -637,6 +622,7 @@ impl From<Key> for xkeysym::Keysym {
#[allow(clippy::match_same_arms)]
match key {
Key::Unicode(c) => xkeysym::Keysym::from_char(c),
Key::Add => Keysym::KP_Add,
Key::Alt | Key::Option => Keysym::Alt_L,
Key::Backspace => Keysym::BackSpace,
Key::Begin => Keysym::Begin,
Expand All @@ -645,7 +631,9 @@ impl From<Key> for xkeysym::Keysym {
Key::CapsLock => Keysym::Caps_Lock,
Key::Clear => Keysym::Clear,
Key::Control | Key::LControl => Keysym::Control_L,
Key::Decimal => Keysym::KP_Decimal,
Key::Delete => Keysym::Delete,
Key::Divide => Keysym::KP_Divide,
Key::DownArrow => Keysym::Down,
Key::End => Keysym::End,
Key::Escape => Keysym::Escape,
Expand Down Expand Up @@ -696,11 +684,22 @@ impl From<Key> for xkeysym::Keysym {
Key::Linefeed => Keysym::Linefeed,
Key::LMenu => Keysym::Menu,
Key::ModeChange => Keysym::Mode_switch,
Key::Multiply => Keysym::KP_Multiply,
Key::MediaNextTrack => Keysym::XF86_AudioNext,
Key::MediaPlayPause => Keysym::XF86_AudioPlay,
Key::MediaPrevTrack => Keysym::XF86_AudioPrev,
Key::MediaStop => Keysym::XF86_AudioStop,
Key::Numlock => Keysym::Num_Lock,
Key::Numpad0 => Keysym::KP_0,
Key::Numpad1 => Keysym::KP_1,
Key::Numpad2 => Keysym::KP_2,
Key::Numpad3 => Keysym::KP_3,
Key::Numpad4 => Keysym::KP_4,
Key::Numpad5 => Keysym::KP_5,
Key::Numpad6 => Keysym::KP_6,
Key::Numpad7 => Keysym::KP_7,
Key::Numpad8 => Keysym::KP_8,
Key::Numpad9 => Keysym::KP_9,
Key::PageDown => Keysym::Page_Down,
Key::PageUp => Keysym::Page_Up,
Key::Pause => Keysym::Pause,
Expand All @@ -717,6 +716,7 @@ impl From<Key> for xkeysym::Keysym {
Key::Shift | Key::LShift => Keysym::Shift_L,
Key::ShiftLock => Keysym::Shift_Lock,
Key::Space => Keysym::space,
Key::Subtract => Keysym::KP_Subtract,
Key::SysReq => Keysym::Sys_Req,
Key::Tab => Keysym::Tab,
Key::Undo => Keysym::Undo,
Expand Down
35 changes: 35 additions & 0 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ use crate::{
NewConError, Settings,
};

// TODO: Replace with upstream values once a new version of core_graphics is published that contains https://github.com/servo/core-foundation-rs/pull/712
const ANSI_KEYPAD_0: u16 = 0x52;
const ANSI_KEYPAD_1: u16 = 0x53;
const ANSI_KEYPAD_2: u16 = 0x54;
const ANSI_KEYPAD_3: u16 = 0x55;
const ANSI_KEYPAD_4: u16 = 0x56;
const ANSI_KEYPAD_5: u16 = 0x57;
const ANSI_KEYPAD_6: u16 = 0x58;
const ANSI_KEYPAD_7: u16 = 0x59;
const ANSI_KEYPAD_8: u16 = 0x5B;
const ANSI_KEYPAD_9: u16 = 0x5C;
const ANSI_KEYPAD_DECIMAL: u16 = 0x41;
const ANSI_KEYPAD_MULTIPLY: u16 = 0x43;
const ANSI_KEYPAD_PLUS: u16 = 0x45;
const ANSI_KEYPAD_CLEAR: u16 = 0x47;
const ANSI_KEYPAD_DIVIDE: u16 = 0x4B;
const ANSI_KEYPAD_ENTER: u16 = 0x4C;
const ANSI_KEYPAD_MINUS: u16 = 0x4E;
const ANSI_KEYPAD_EQUAL: u16 = 0x51;

#[repr(C)]
struct __TISInputSource;
type TISInputSourceRef = *const __TISInputSource;
Expand Down Expand Up @@ -913,11 +933,14 @@ impl TryFrom<Key> for core_graphics::event::CGKeyCode {
// https://docs.rs/core-graphics/latest/core_graphics/event/struct.KeyCode.html
// https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
let key = match key {
Key::Add => ANSI_KEYPAD_PLUS,
Key::Alt | Key::Option => KeyCode::OPTION,
Key::Backspace => KeyCode::DELETE,
Key::CapsLock => KeyCode::CAPS_LOCK,
Key::Control | Key::LControl => KeyCode::CONTROL,
Key::Decimal => ANSI_KEYPAD_DECIMAL,
Key::Delete => KeyCode::FORWARD_DELETE,
Key::Divide => ANSI_KEYPAD_DIVIDE,
Key::DownArrow => KeyCode::DOWN_ARROW,
Key::End => KeyCode::END,
Key::Escape => KeyCode::ESCAPE,
Expand Down Expand Up @@ -947,6 +970,17 @@ impl TryFrom<Key> for core_graphics::event::CGKeyCode {
Key::Launchpad => 131,
Key::LeftArrow => KeyCode::LEFT_ARROW,
Key::MissionControl => 160,
Key::Multiply => ANSI_KEYPAD_MULTIPLY,
Key::Numpad0 => ANSI_KEYPAD_0,
Key::Numpad1 => ANSI_KEYPAD_1,
Key::Numpad2 => ANSI_KEYPAD_2,
Key::Numpad3 => ANSI_KEYPAD_3,
Key::Numpad4 => ANSI_KEYPAD_4,
Key::Numpad5 => ANSI_KEYPAD_5,
Key::Numpad6 => ANSI_KEYPAD_6,
Key::Numpad7 => ANSI_KEYPAD_7,
Key::Numpad8 => ANSI_KEYPAD_8,
Key::Numpad9 => ANSI_KEYPAD_9,
Key::PageDown => KeyCode::PAGE_DOWN,
Key::PageUp => KeyCode::PAGE_UP,
Key::RCommand => KeyCode::RIGHT_COMMAND,
Expand All @@ -957,6 +991,7 @@ impl TryFrom<Key> for core_graphics::event::CGKeyCode {
Key::ROption => KeyCode::RIGHT_OPTION,
Key::Shift | Key::LShift => KeyCode::SHIFT,
Key::Space => KeyCode::SPACE,
Key::Subtract => ANSI_KEYPAD_MINUS,
Key::Tab => KeyCode::TAB,
Key::UpArrow => KeyCode::UP_ARROW,
Key::VolumeDown => KeyCode::VOLUME_DOWN,
Expand Down