rio_window/platform/scancode.rs
1use crate::keyboard::{KeyCode, PhysicalKey};
2
3// TODO: Describe what this value contains for each platform
4
5/// Additional methods for the [`PhysicalKey`] type that allow the user to access the
6/// platform-specific scancode.
7///
8/// [`PhysicalKey`]: crate::keyboard::PhysicalKey
9pub trait PhysicalKeyExtScancode {
10 /// The raw value of the platform-specific physical key identifier.
11 ///
12 /// Returns `Some(key_id)` if the conversion was successful; returns `None` otherwise.
13 ///
14 /// ## Platform-specific
15 /// - **Windows:** A 16bit extended scancode
16 /// - **Wayland/X11**: A 32-bit linux scancode, which is X11/Wayland keycode subtracted by 8.
17 fn to_scancode(self) -> Option<u32>;
18
19 /// Constructs a `PhysicalKey` from a platform-specific physical key identifier.
20 ///
21 /// Note that this conversion may be lossy, i.e. converting the returned `PhysicalKey` back
22 /// using `to_scancode` might not yield the original value.
23 ///
24 /// ## Platform-specific
25 /// - **Wayland/X11**: A 32-bit linux scancode. When building from X11/Wayland keycode subtract
26 /// `8` to get the value you wanted.
27 fn from_scancode(scancode: u32) -> PhysicalKey;
28}
29
30impl PhysicalKeyExtScancode for PhysicalKey {
31 fn to_scancode(self) -> Option<u32> {
32 crate::platform_impl::physicalkey_to_scancode(self)
33 }
34
35 fn from_scancode(scancode: u32) -> PhysicalKey {
36 crate::platform_impl::scancode_to_physicalkey(scancode)
37 }
38}
39
40impl PhysicalKeyExtScancode for KeyCode {
41 #[inline]
42 fn to_scancode(self) -> Option<u32> {
43 <PhysicalKey as PhysicalKeyExtScancode>::to_scancode(PhysicalKey::Code(self))
44 }
45
46 #[inline]
47 fn from_scancode(scancode: u32) -> PhysicalKey {
48 <PhysicalKey as PhysicalKeyExtScancode>::from_scancode(scancode)
49 }
50}