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