raw_input/
event.rs

1#[cfg(feature = "serialize")]
2use serde::{Deserialize, Serialize};
3
4/// Represents the standard buttons on a mouse.
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
7pub enum MouseButton {
8    Left,
9    Right,
10    Middle,
11    Back,
12    Forward,
13}
14
15/// A simple coordinate point using integers, typically for pixel positions.
16#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
17#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
18pub struct Point {
19    pub x: i32,
20    pub y: i32,
21}
22
23/// A coordinate point using floating-point numbers, used for precise deltas or scaling.
24#[derive(Debug, Copy, Clone, PartialEq)]
25#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
26pub struct FloatPoint {
27    pub x: f64,
28    pub y: f64,
29}
30
31/// Platform-specific key code type.
32#[cfg(not(target_os = "macos"))]
33pub type KeyCode = u32;
34// #[cfg(target_os = "macos")]
35// pub type KeyCode = crate::CGKeyCode;
36
37/// Represents raw hardware scan codes or virtual key codes from different OS layers.
38#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
39#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
40pub enum RawKey {
41    ScanCode(KeyCode),
42    WinVirtualKeycode(KeyCode),
43    LinuxXorgKeycode(KeyCode),
44    LinuxConsoleKeycode(KeyCode),
45    MacVirtualKeycode(KeyCode),
46}
47
48#[rustfmt::skip]
49/// A high-level representation of keyboard keys.
50#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
51#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
52pub enum Key {
53    /// Alt key on Linux and Windows (option key on macOS)
54    Alt,
55    AltGr,
56    Backspace,
57    CapsLock,
58    ControlLeft,
59    ControlRight,
60    Delete,
61    DownArrow,
62    End,
63    Escape,
64    F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24,
65    Home,
66    LeftArrow,
67    /// Also known as "Windows", "Super", or "Command"
68    MetaLeft,
69    /// Also known as "Windows", "Super", or "Command"
70    MetaRight,
71    PageDown,
72    PageUp,
73    Return,
74    RightArrow,
75    ShiftLeft,
76    ShiftRight,
77    Space,
78    Tab,
79    UpArrow,
80    PrintScreen,
81    ScrollLock,
82    Pause,
83    NumLock,
84    BackQuote,
85    Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9,
86    Minus,
87    Equal,
88    KeyQ, KeyW, KeyE, KeyR, KeyT, KeyY, KeyU, KeyI, KeyO, KeyP,
89    LeftBracket,
90    RightBracket,
91    KeyA, KeyS, KeyD, KeyF, KeyG, KeyH, KeyJ, KeyK, KeyL,
92    SemiColon,
93    Quote,
94    BackSlash,
95    IntlBackslash,
96    IntlRo,   // Brazilian /? and Japanese _ 'ro'
97    IntlYen,  // Japanese Henkan (Convert) key.
98    KanaMode, // Japanese Hiragana/Katakana key.
99    KeyZ, KeyX, KeyC, KeyV, KeyB, KeyN, KeyM,
100    Comma,
101    Dot,
102    Slash,
103    Insert,
104    KpReturn,
105    KpMinus,
106    KpPlus,
107    KpMultiply,
108    KpDivide,
109    KpDecimal,
110    KpEqual,
111    KpComma,
112    Kp0, Kp1, Kp2, Kp3, Kp4, Kp5, Kp6, Kp7, Kp8, Kp9,
113    VolumeUp,
114    VolumeDown,
115    VolumeMute,
116    Lang1, // Korean Hangul/English toggle key, and as the Kana key on the Apple Japanese keyboard.
117    Lang2, // Korean Hanja conversion key, and as the Eisu key on the Apple Japanese keyboard.
118    Lang3, // Japanese Katakana key.
119    Lang4, // Japanese Hiragana key.
120    Lang5, // Japanese Zenkaku/Hankaku (Fullwidth/halfwidth) key.
121    Function,
122    Apps,
123    Cancel,
124    Clear,
125    Kana,
126    Hangul,
127    Junja,
128    Final,
129    Hanja,
130    Hanji,
131    Print,
132    Select,
133    Execute,
134    Help,
135    Sleep,
136    Separator,
137    Unknown(u32),
138    RawKey(RawKey),
139}
140
141/// The main event enum containing all possible input actions.
142///
143/// # Example
144/// ```
145/// use raw_input::{Event, Key};
146///
147/// fn handle_event(event: Event) {
148///     match event {
149///         Event::KeyDown { key: Key::Escape } => println!("Escape pressed!"),
150///         _ => {}
151///     }
152/// }
153/// ```
154#[derive(Debug, Copy, Clone, PartialEq)]
155#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
156pub enum Event {
157    /// Mouse movement with pixel delta.
158    MouseMove { delta: Point },
159    /// Mouse wheel rotation with floating point precision.
160    MouseWheel { delta: FloatPoint },
161    /// Mouse button press.
162    MouseDown { button: MouseButton },
163    /// Mouse button release.
164    MouseUp { button: MouseButton },
165    /// Keyboard key press.
166    KeyDown { key: Key },
167    /// Keyboard key release.
168    KeyUp { key: Key },
169}