From decfc3d92c0bfbaddea031990eb135a5a4174734 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 1 Dec 2024 21:14:55 +0100 Subject: [PATCH] Linux: Align scancode conversions with Firefox --- src/changelog/unreleased.md | 1 + src/platform_impl/linux/common/xkb/keymap.rs | 72 +++++++++++++------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 4e91f91773..4c8616e158 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -75,6 +75,7 @@ changelog entry. variables to test the respective modifiers of window creation. - Added `Window::surface_position`, which is the position of the surface inside the window. - Added `Window::safe_area`, which describes the area of the surface that is unobstructed. +- On X11 and Wayland, improved scancode conversions for more obscure key codes. ### Changed diff --git a/src/platform_impl/linux/common/xkb/keymap.rs b/src/platform_impl/linux/common/xkb/keymap.rs index 168b8b25e7..d9ac1838ef 100644 --- a/src/platform_impl/linux/common/xkb/keymap.rs +++ b/src/platform_impl/linux/common/xkb/keymap.rs @@ -35,6 +35,9 @@ pub fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey { // are defined by the Linux kernel. If Winit programs end up being run on other Unix-likes, // I can only hope they agree on what the keycodes mean. // + // The mapping here is heavily influenced by Firefox' source: + // https://searchfox.org/mozilla-central/rev/c597e9c789ad36af84a0370d395be066b7dc94f4/widget/NativeKeyToDOMCodeName.h + // // Some of the keycodes are likely superfluous for our purposes, and some are ones which are // difficult to test the correctness of, or discover the purpose of. Because of this, they've // either been commented out here, or not included at all. @@ -166,23 +169,23 @@ pub fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey { 125 => KeyCode::SuperLeft, 126 => KeyCode::SuperRight, 127 => KeyCode::ContextMenu, - // 128 => KeyCode::STOP, - // 129 => KeyCode::AGAIN, - // 130 => KeyCode::PROPS, - // 131 => KeyCode::UNDO, - // 132 => KeyCode::FRONT, - // 133 => KeyCode::COPY, - // 134 => KeyCode::OPEN, - // 135 => KeyCode::PASTE, - // 136 => KeyCode::FIND, - // 137 => KeyCode::CUT, - // 138 => KeyCode::HELP, + 128 => KeyCode::BrowserStop, + 129 => KeyCode::Again, + 130 => KeyCode::Props, + 131 => KeyCode::Undo, + 132 => KeyCode::Select, // FRONT + 133 => KeyCode::Copy, + 134 => KeyCode::Open, + 135 => KeyCode::Paste, + 136 => KeyCode::Find, + 137 => KeyCode::Cut, + 138 => KeyCode::Help, // 139 => KeyCode::MENU, - // 140 => KeyCode::CALC, + 140 => KeyCode::LaunchApp2, // CALC // 141 => KeyCode::SETUP, // 142 => KeyCode::SLEEP, - // 143 => KeyCode::WAKEUP, - // 144 => KeyCode::FILE, + 143 => KeyCode::WakeUp, + 144 => KeyCode::LaunchApp1, // FILE // 145 => KeyCode::SENDFILE, // 146 => KeyCode::DELETEFILE, // 147 => KeyCode::XFER, @@ -193,13 +196,13 @@ pub fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey { // 152 => KeyCode::COFFEE, // 153 => KeyCode::ROTATE_DISPLAY, // 154 => KeyCode::CYCLEWINDOWS, - // 155 => KeyCode::MAIL, - // 156 => KeyCode::BOOKMARKS, + 155 => KeyCode::LaunchMail, + 156 => KeyCode::BrowserFavorites, // BOOKMARKS // 157 => KeyCode::COMPUTER, - // 158 => KeyCode::BACK, - // 159 => KeyCode::FORWARD, + 158 => KeyCode::BrowserBack, + 159 => KeyCode::BrowserForward, // 160 => KeyCode::CLOSECD, - // 161 => KeyCode::EJECTCD, + 161 => KeyCode::Eject, // EJECTCD // 162 => KeyCode::EJECTCLOSECD, 163 => KeyCode::MediaTrackNext, 164 => KeyCode::MediaPlayPause, @@ -209,9 +212,9 @@ pub fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey { // 168 => KeyCode::REWIND, // 169 => KeyCode::PHONE, // 170 => KeyCode::ISO, - // 171 => KeyCode::CONFIG, - // 172 => KeyCode::HOMEPAGE, - // 173 => KeyCode::REFRESH, + 171 => KeyCode::MediaSelect, // CONFIG + 172 => KeyCode::BrowserHome, + 173 => KeyCode::BrowserRefresh, // 174 => KeyCode::EXIT, // 175 => KeyCode::MOVE, // 176 => KeyCode::EDIT, @@ -250,7 +253,7 @@ pub fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey { // 214 => KeyCode::QUESTION, // 215 => KeyCode::EMAIL, // 216 => KeyCode::CHAT, - // 217 => KeyCode::SEARCH, + 217 => KeyCode::BrowserSearch, // 218 => KeyCode::CONNECT, // 219 => KeyCode::FINANCE, // 220 => KeyCode::SPORT, @@ -419,10 +422,32 @@ pub fn physicalkey_to_scancode(key: PhysicalKey) -> Option { KeyCode::SuperLeft => Some(125), KeyCode::SuperRight => Some(126), KeyCode::ContextMenu => Some(127), + KeyCode::BrowserStop => Some(128), + KeyCode::Again => Some(129), + KeyCode::Props => Some(130), + KeyCode::Undo => Some(131), + KeyCode::Select => Some(132), + KeyCode::Copy => Some(133), + KeyCode::Open => Some(134), + KeyCode::Paste => Some(135), + KeyCode::Find => Some(136), + KeyCode::Cut => Some(137), + KeyCode::Help => Some(138), + KeyCode::LaunchApp2 => Some(140), + KeyCode::WakeUp => Some(143), + KeyCode::LaunchApp1 => Some(144), + KeyCode::LaunchMail => Some(155), + KeyCode::BrowserFavorites => Some(156), + KeyCode::BrowserBack => Some(158), + KeyCode::BrowserForward => Some(159), + KeyCode::Eject => Some(161), KeyCode::MediaTrackNext => Some(163), KeyCode::MediaPlayPause => Some(164), KeyCode::MediaTrackPrevious => Some(165), KeyCode::MediaStop => Some(166), + KeyCode::MediaSelect => Some(171), + KeyCode::BrowserHome => Some(172), + KeyCode::BrowserRefresh => Some(173), KeyCode::F13 => Some(183), KeyCode::F14 => Some(184), KeyCode::F15 => Some(185), @@ -435,6 +460,7 @@ pub fn physicalkey_to_scancode(key: PhysicalKey) -> Option { KeyCode::F22 => Some(192), KeyCode::F23 => Some(193), KeyCode::F24 => Some(194), + KeyCode::BrowserSearch => Some(217), _ => None, } }