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

Support for more macOS keycodes #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions src/macos/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const F6: CGKeyCode = 97;
const F7: CGKeyCode = 98;
const F8: CGKeyCode = 100;
const F9: CGKeyCode = 101;
const F13: CGKeyCode = 105;
const F14: CGKeyCode = 107;
const F15: CGKeyCode = 113;
const F16: CGKeyCode = 106;
const F17: CGKeyCode = 64;
const F18: CGKeyCode = 79;
const F19: CGKeyCode = 80;
const F20: CGKeyCode = 90;
const FUNCTION: CGKeyCode = 63;
const LEFT_ARROW: CGKeyCode = 123;
const META_LEFT: CGKeyCode = 55;
Expand Down Expand Up @@ -82,6 +90,21 @@ const KEY_M: CGKeyCode = 46;
const COMMA: CGKeyCode = 43;
const DOT: CGKeyCode = 47;
const SLASH: CGKeyCode = 44;
const KP_RETURN: CGKeyCode = 76;
const KP_MINUS: CGKeyCode = 78;
const KP_PLUS: CGKeyCode = 69;
const KP_MULTIPLY: CGKeyCode = 67;
const KP_DIVIDE: CGKeyCode = 75;
const KP0: CGKeyCode = 82;
const KP1: CGKeyCode = 83;
const KP2: CGKeyCode = 84;
const KP3: CGKeyCode = 85;
const KP4: CGKeyCode = 86;
const KP5: CGKeyCode = 87;
const KP6: CGKeyCode = 88;
const KP7: CGKeyCode = 89;
const KP8: CGKeyCode = 91;
const KP9: CGKeyCode = 92;

pub fn code_from_key(key: Key) -> Option<CGKeyCode> {
match key {
Expand All @@ -105,6 +128,14 @@ pub fn code_from_key(key: Key) -> Option<CGKeyCode> {
Key::F7 => Some(F7),
Key::F8 => Some(F8),
Key::F9 => Some(F9),
Key::F13 => Some(F13),
Key::F14 => Some(F14),
Key::F15 => Some(F15),
Key::F16 => Some(F16),
Key::F17 => Some(F17),
Key::F18 => Some(F18),
Key::F19 => Some(F19),
Key::F20 => Some(F20),
Key::LeftArrow => Some(LEFT_ARROW),
Key::MetaLeft => Some(META_LEFT),
Key::MetaRight => Some(META_RIGHT),
Expand Down Expand Up @@ -162,6 +193,21 @@ pub fn code_from_key(key: Key) -> Option<CGKeyCode> {
Key::Comma => Some(COMMA),
Key::Dot => Some(DOT),
Key::Slash => Some(SLASH),
Key::Kp1 => Some(KP1),
Key::Kp2 => Some(KP2),
Key::Kp3 => Some(KP3),
Key::Kp4 => Some(KP4),
Key::Kp5 => Some(KP5),
Key::Kp6 => Some(KP6),
Key::Kp7 => Some(KP7),
Key::Kp8 => Some(KP8),
Key::Kp9 => Some(KP9),
Key::Kp0 => Some(KP0),
Key::KpReturn => Some(KP_RETURN),
Key::KpMinus => Some(KP_MINUS),
Key::KpPlus => Some(KP_PLUS),
Key::KpMultiply => Some(KP_MULTIPLY),
Key::KpDivide => Some(KP_DIVIDE),
Key::Function => Some(FUNCTION),
Key::Unknown(code) => code.try_into().ok(),
_ => None,
Expand All @@ -175,6 +221,7 @@ pub fn key_from_code(code: CGKeyCode) -> Key {
BACKSPACE => Key::Backspace,
CAPS_LOCK => Key::CapsLock,
CONTROL_LEFT => Key::ControlLeft,
CONTROL_RIGHT => Key::ControlRight,
DOWN_ARROW => Key::DownArrow,
ESCAPE => Key::Escape,
F1 => Key::F1,
Expand All @@ -189,6 +236,14 @@ pub fn key_from_code(code: CGKeyCode) -> Key {
F7 => Key::F7,
F8 => Key::F8,
F9 => Key::F9,
F13 => Key::F13,
F14 => Key::F14,
F15 => Key::F15,
F16 => Key::F16,
F17 => Key::F17,
F18 => Key::F18,
F19 => Key::F19,
F20 => Key::F20,
LEFT_ARROW => Key::LeftArrow,
META_LEFT => Key::MetaLeft,
META_RIGHT => Key::MetaRight,
Expand Down Expand Up @@ -246,6 +301,21 @@ pub fn key_from_code(code: CGKeyCode) -> Key {
COMMA => Key::Comma,
DOT => Key::Dot,
SLASH => Key::Slash,
KP1 => Key::Kp1,
KP2 => Key::Kp2,
KP3 => Key::Kp3,
KP4 => Key::Kp4,
KP5 => Key::Kp5,
KP6 => Key::Kp6,
KP7 => Key::Kp7,
KP8 => Key::Kp8,
KP9 => Key::Kp9,
KP0 => Key::Kp0,
KP_RETURN => Key::KpReturn,
KP_MINUS => Key::KpMinus,
KP_PLUS => Key::KpPlus,
KP_MULTIPLY => Key::KpMultiply,
KP_DIVIDE => Key::KpDivide,
FUNCTION => Key::Function,
code => Key::Unknown(code.into()),
}
Expand Down