Skip to content

Commit 7be0d46

Browse files
benfacenot-fl3
authored andcommitted
android: map the rest of the hardware-keyboard keycodes
The translation table only covered letters, digits, arrows, Enter, Tab, Space, Backspace, Shift / Alt, comma + period — which means a hardware keyboard on Android (tablet + Bluetooth / USB / Magic Keyboard; ChromeOS; Stage Manager iPad with a Mac keyboard) couldn't reach the host app with most of its keys. Adds the keys that have a clear semantic and a matching `KeyCode` variant: - Modifiers: Ctrl L/R, Meta L/R (Cmd on Apple keyboards), CapsLock, NumLock, ScrollLock. PR #403 already wired the `KeyMods` tracking for Ctrl / Super, but that's a no-op as long as the keycodes themselves translate to `KeyCode::Unknown`. - Edit cluster: Escape, Insert, Delete (`KEYCODE_FORWARD_DEL`, distinct from the existing `KEYCODE_DEL` → Backspace), MoveHome, MoveEnd, PageUp, PageDown, PrintScreen / Pause. - Punctuation: `=`, `[`, `]`, `\\`, `;`, `/`, backtick. - Menu (KEYCODE_MENU, the soft-menu / context-menu key on tablets). - F1–F12. - Numpad: 0–9 + the four arithmetic ops + dot + Enter + equals. Keeps the existing `KEYCODE_HOME` (0x03) entry that maps the device home button to `KeyCode::Home`; in practice that keycode doesn't reach app input anyway and the new `KEYCODE_MOVE_HOME` (0x7a) maps to the same variant for the cursor-home case. Verified on a Pixel Tablet AVD against `is_key_pressed(Escape)`, `is_key_down(LeftControl)`, and `is_key_pressed(KpEnter)`.
1 parent e188d7e commit 7be0d46

1 file changed

Lines changed: 89 additions & 33 deletions

File tree

src/native/android/keycodes.rs

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
use crate::event::KeyCode;
22

3+
/// Translate an Android `AKEYCODE_*` value into miniquad's
4+
/// [`KeyCode`]. Keep entries ordered by numeric value; the comments
5+
/// reference the `KEYCODE_*` symbol from Android's `KeyEvent.java`
6+
/// so each line is self-explanatory at a glance.
37
pub fn translate_keycode(keycode: u32) -> KeyCode {
4-
// same as GLFW
58
match keycode {
6-
0x01 => KeyCode::Left,
7-
0x02 => KeyCode::Right,
8-
0x03 => KeyCode::Home,
9-
0x04 => KeyCode::Back,
10-
0x07 => KeyCode::Key0,
11-
0x08 => KeyCode::Key1,
12-
0x09 => KeyCode::Key2,
13-
0x0a => KeyCode::Key3,
14-
0x0b => KeyCode::Key4,
15-
0x0c => KeyCode::Key5,
16-
0x0d => KeyCode::Key6,
17-
0x0e => KeyCode::Key7,
18-
0x0f => KeyCode::Key8,
19-
0x10 => KeyCode::Key9,
20-
0x13 => KeyCode::Up,
21-
0x14 => KeyCode::Down,
22-
0x15 => KeyCode::Left,
23-
0x16 => KeyCode::Right,
24-
0x17 => KeyCode::Enter,
25-
0x1d => KeyCode::A,
9+
0x01 => KeyCode::Left, // KEYCODE_SOFT_LEFT
10+
0x02 => KeyCode::Right, // KEYCODE_SOFT_RIGHT
11+
0x03 => KeyCode::Home, // KEYCODE_HOME (device home button)
12+
0x04 => KeyCode::Back, // KEYCODE_BACK
13+
0x07 => KeyCode::Key0, // KEYCODE_0
14+
0x08 => KeyCode::Key1, // KEYCODE_1
15+
0x09 => KeyCode::Key2, // KEYCODE_2
16+
0x0a => KeyCode::Key3, // KEYCODE_3
17+
0x0b => KeyCode::Key4, // KEYCODE_4
18+
0x0c => KeyCode::Key5, // KEYCODE_5
19+
0x0d => KeyCode::Key6, // KEYCODE_6
20+
0x0e => KeyCode::Key7, // KEYCODE_7
21+
0x0f => KeyCode::Key8, // KEYCODE_8
22+
0x10 => KeyCode::Key9, // KEYCODE_9
23+
0x13 => KeyCode::Up, // KEYCODE_DPAD_UP
24+
0x14 => KeyCode::Down, // KEYCODE_DPAD_DOWN
25+
0x15 => KeyCode::Left, // KEYCODE_DPAD_LEFT
26+
0x16 => KeyCode::Right, // KEYCODE_DPAD_RIGHT
27+
0x17 => KeyCode::Enter, // KEYCODE_DPAD_CENTER
28+
0x1d => KeyCode::A, // KEYCODE_A
2629
0x1e => KeyCode::B,
2730
0x1f => KeyCode::C,
2831
0x20 => KeyCode::D,
@@ -47,19 +50,72 @@ pub fn translate_keycode(keycode: u32) -> KeyCode {
4750
0x33 => KeyCode::W,
4851
0x34 => KeyCode::X,
4952
0x35 => KeyCode::Y,
50-
0x36 => KeyCode::Z,
51-
0x37 => KeyCode::Comma,
52-
0x38 => KeyCode::Period,
53-
0x39 => KeyCode::LeftAlt,
54-
0x3a => KeyCode::RightAlt,
55-
0x3b => KeyCode::LeftShift,
56-
0x3c => KeyCode::RightShift,
57-
0x3d => KeyCode::Tab,
58-
0x3e => KeyCode::Space,
59-
0x42 => KeyCode::Enter,
60-
// android calls it Delete, but it has an icon of the Backspace and
61-
// expected behavior of the Backspace
53+
0x36 => KeyCode::Z, // KEYCODE_Z
54+
0x37 => KeyCode::Comma, // KEYCODE_COMMA
55+
0x38 => KeyCode::Period, // KEYCODE_PERIOD
56+
0x39 => KeyCode::LeftAlt, // KEYCODE_ALT_LEFT
57+
0x3a => KeyCode::RightAlt, // KEYCODE_ALT_RIGHT
58+
0x3b => KeyCode::LeftShift, // KEYCODE_SHIFT_LEFT
59+
0x3c => KeyCode::RightShift, // KEYCODE_SHIFT_RIGHT
60+
0x3d => KeyCode::Tab, // KEYCODE_TAB
61+
0x3e => KeyCode::Space, // KEYCODE_SPACE
62+
0x42 => KeyCode::Enter, // KEYCODE_ENTER
63+
// KEYCODE_DEL — Android names it "Delete" but the key has the
64+
// Backspace icon + behaviour.
6265
0x43 => KeyCode::Backspace,
66+
0x44 => KeyCode::GraveAccent, // KEYCODE_GRAVE
67+
0x46 => KeyCode::Equal, // KEYCODE_EQUALS
68+
0x47 => KeyCode::LeftBracket, // KEYCODE_LEFT_BRACKET
69+
0x48 => KeyCode::RightBracket, // KEYCODE_RIGHT_BRACKET
70+
0x49 => KeyCode::Backslash, // KEYCODE_BACKSLASH
71+
0x4a => KeyCode::Semicolon, // KEYCODE_SEMICOLON
72+
0x4c => KeyCode::Slash, // KEYCODE_SLASH
73+
0x52 => KeyCode::Menu, // KEYCODE_MENU
74+
0x5c => KeyCode::PageUp, // KEYCODE_PAGE_UP
75+
0x5d => KeyCode::PageDown, // KEYCODE_PAGE_DOWN
76+
0x6f => KeyCode::Escape, // KEYCODE_ESCAPE
77+
0x70 => KeyCode::Delete, // KEYCODE_FORWARD_DEL
78+
0x71 => KeyCode::LeftControl, // KEYCODE_CTRL_LEFT
79+
0x72 => KeyCode::RightControl, // KEYCODE_CTRL_RIGHT
80+
0x73 => KeyCode::CapsLock, // KEYCODE_CAPS_LOCK
81+
0x74 => KeyCode::ScrollLock, // KEYCODE_SCROLL_LOCK
82+
0x75 => KeyCode::LeftSuper, // KEYCODE_META_LEFT
83+
0x76 => KeyCode::RightSuper, // KEYCODE_META_RIGHT
84+
0x78 => KeyCode::PrintScreen, // KEYCODE_SYSRQ
85+
0x79 => KeyCode::Pause, // KEYCODE_BREAK
86+
0x7a => KeyCode::Home, // KEYCODE_MOVE_HOME (cursor home)
87+
0x7b => KeyCode::End, // KEYCODE_MOVE_END
88+
0x7c => KeyCode::Insert, // KEYCODE_INSERT
89+
0x83 => KeyCode::F1, // KEYCODE_F1
90+
0x84 => KeyCode::F2,
91+
0x85 => KeyCode::F3,
92+
0x86 => KeyCode::F4,
93+
0x87 => KeyCode::F5,
94+
0x88 => KeyCode::F6,
95+
0x89 => KeyCode::F7,
96+
0x8a => KeyCode::F8,
97+
0x8b => KeyCode::F9,
98+
0x8c => KeyCode::F10,
99+
0x8d => KeyCode::F11,
100+
0x8e => KeyCode::F12, // KEYCODE_F12
101+
0x8f => KeyCode::NumLock, // KEYCODE_NUM_LOCK
102+
0x90 => KeyCode::Kp0, // KEYCODE_NUMPAD_0
103+
0x91 => KeyCode::Kp1,
104+
0x92 => KeyCode::Kp2,
105+
0x93 => KeyCode::Kp3,
106+
0x94 => KeyCode::Kp4,
107+
0x95 => KeyCode::Kp5,
108+
0x96 => KeyCode::Kp6,
109+
0x97 => KeyCode::Kp7,
110+
0x98 => KeyCode::Kp8,
111+
0x99 => KeyCode::Kp9, // KEYCODE_NUMPAD_9
112+
0x9a => KeyCode::KpDivide, // KEYCODE_NUMPAD_DIVIDE
113+
0x9b => KeyCode::KpMultiply, // KEYCODE_NUMPAD_MULTIPLY
114+
0x9c => KeyCode::KpSubtract, // KEYCODE_NUMPAD_SUBTRACT
115+
0x9d => KeyCode::KpAdd, // KEYCODE_NUMPAD_ADD
116+
0x9e => KeyCode::KpDecimal, // KEYCODE_NUMPAD_DOT
117+
0xa0 => KeyCode::KpEnter, // KEYCODE_NUMPAD_ENTER
118+
0xa1 => KeyCode::KpEqual, // KEYCODE_NUMPAD_EQUALS
63119
_ => KeyCode::Unknown,
64120
}
65121
}

0 commit comments

Comments
 (0)