Skip to main content

willhook/
event.rs

1pub(super) mod details;
2
3#[cfg(feature = "serde")]
4use serde::{Serialize, Deserialize};
5
6/// Main event sent by the hook to the client thread.
7#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum InputEvent {
10    /// It is keyboard event, the inner value contains the details. See [KeyboardEvent].
11    Keyboard(KeyboardEvent),
12    /// It is mouse event, the inner value contains the details. See [MouseEvent].
13    Mouse(MouseEvent),
14    /// Unexpected data was received by the hook, the event type is stored for reference as inner value.
15    Other(u32),
16}
17
18/// Indicates if the keyboard event was injected by the software, see this crate integration tests for example.
19#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
20#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21pub enum IsEventInjected {
22    /// Event was injected by software
23    Injected,
24    /// Input comes from the user input (real hardware)
25    NotInjected,
26}
27
28/// Keyboard event with data if key was pressed down or up, what key was pressed, and if event was injected. 
29#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
30#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31pub struct KeyboardEvent {
32    /// Indicates if this is a press or release
33    pub pressed: KeyPress,
34    /// Code of the key that triggered an event
35    pub key: Option<KeyboardKey>,
36    /// If the event was injected by the software
37    pub is_injected: Option<IsEventInjected>,
38}
39
40/// Enum to distinguish system key press from normal key press.
41#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
42#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
43pub enum IsSystemKeyPress {
44    /// System key is basically any key pressed while ALT is also pressed
45    System,
46    /// Indicates that key input event occured while ALT key was NOT pressed
47    Normal,
48}
49
50/// Indicates whether the [KeyboardKey] was pressed [KeyPress::Down] or [KeyPress::Up].
51#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
52#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
53pub enum KeyPress {
54    /// Pressed down
55    Down(IsSystemKeyPress),
56    /// Released
57    Up(IsSystemKeyPress),
58    Other(usize),
59}
60
61/// Indicates key on the keyboard.
62#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
63#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
64pub enum KeyboardKey {
65    A,
66    B,
67    C,
68    D,
69    E,
70    F,
71    G,
72    H,
73    I,
74    J,
75    K,
76    L,
77    M,
78    N,
79    O,
80    P,
81    Q,
82    R,
83    S,
84    T,
85    U,
86    V,
87    W,
88    X,
89    Y,
90    Z,
91    Number0,
92    Number1,
93    Number2,
94    Number3,
95    Number4,
96    Number5,
97    Number6,
98    Number7,
99    Number8,
100    Number9,
101    LeftAlt,
102    RightAlt,
103    LeftShift,
104    RightShift,
105    LeftControl,
106    RightControl,
107    BackSpace,
108    Tab,
109    Enter,
110    Escape,
111    Space,
112    PageUp,
113    PageDown,
114    Home,
115    ArrowLeft,
116    ArrowUp,
117    ArrowRight,
118    ArrowDown,
119    Print,
120    PrintScreen,
121    Insert,
122    Delete,
123    LeftWindows,
124    RightWindows,
125    /// , (with shift <)
126    Comma,         
127    /// . (with shift >)
128    Period,        
129    /// / (with shift ?)
130    Slash,         
131    /// ; (with shift :)
132    SemiColon,     
133    /// ' (with shift ")
134    Apostrophe,    
135    /// [ (with shift {)
136    LeftBrace,     
137    /// \ (with shift |)
138    BackwardSlash, 
139    /// ] (with shift })
140    RightBrace,    
141    /// ` (with shift ~)
142    Grave,         
143    F1,
144    F2,
145    F3,
146    F4,
147    F5,
148    F6,
149    F7,
150    F8,
151    F9,
152    F10,
153    F11,
154    F12,
155    F13,
156    F14,
157    F15,
158    F16,
159    F17,
160    F18,
161    F19,
162    F20,
163    F21,
164    F22,
165    F23,
166    F24,
167    NumLock,
168    ScrollLock,
169    CapsLock,
170    Numpad0,
171    Numpad1,
172    Numpad2,
173    Numpad3,
174    Numpad4,
175    Numpad5,
176    Numpad6,
177    Numpad7,
178    Numpad8,
179    Numpad9,
180    Multiply,
181    Add,
182    Separator,
183    Subtract,
184    Decimal,
185    Divide,
186    Other(u32),
187    /// Invalid input received from the OS
188    InvalidKeyCodeReceived,
189}
190
191// those dont have defines.
192const VK_0: i32 = 0x30;
193const VK_1: i32 = 0x31;
194const VK_2: i32 = 0x32;
195const VK_3: i32 = 0x33;
196const VK_4: i32 = 0x34;
197const VK_5: i32 = 0x35;
198const VK_6: i32 = 0x36;
199const VK_7: i32 = 0x37;
200const VK_8: i32 = 0x38;
201const VK_9: i32 = 0x39;
202const VK_A: i32 = 0x41;
203const VK_B: i32 = 0x42;
204const VK_C: i32 = 0x43;
205const VK_D: i32 = 0x44;
206const VK_E: i32 = 0x45;
207const VK_F: i32 = 0x46;
208const VK_G: i32 = 0x47;
209const VK_H: i32 = 0x48;
210const VK_I: i32 = 0x49;
211const VK_J: i32 = 0x4A;
212const VK_K: i32 = 0x4B;
213const VK_L: i32 = 0x4C;
214const VK_M: i32 = 0x4D;
215const VK_N: i32 = 0x4E;
216const VK_O: i32 = 0x4F;
217const VK_P: i32 = 0x50;
218const VK_Q: i32 = 0x51;
219const VK_R: i32 = 0x52;
220const VK_S: i32 = 0x53;
221const VK_T: i32 = 0x54;
222const VK_U: i32 = 0x55;
223const VK_V: i32 = 0x56;
224const VK_W: i32 = 0x57;
225const VK_X: i32 = 0x58;
226const VK_Y: i32 = 0x59;
227const VK_Z: i32 = 0x5A;
228
229/// Main mouse event that can be one of [MouseEventType] and also stores if event was injected.
230#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
231#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
232pub struct MouseEvent {
233    /// The enum also stores the particular event data, like position or button
234    pub event: MouseEventType,
235    /// Indicates if event was injected by software
236    pub is_injected: Option<IsEventInjected>,
237}
238
239/// The type of the mouse event with it's specific data
240#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
241#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
242pub enum MouseEventType {
243    /// Button on the mouse was pressed
244    Press(MousePressEvent),
245    /// Mouse was moved
246    Move(MouseMoveEvent),
247    /// Wheel on the mouse was, well, spinning.
248    Wheel(MouseWheelEvent),
249    /// Received unrecognized mouse event type, the code is stored for reference.
250    Other(usize)
251}
252
253/// Holds information which button was pressed or released
254#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
256pub struct MousePressEvent {
257    pub pressed: MouseButtonPress,
258    pub button: MouseButton,
259}
260
261/// Holds information which mouse wheel triggered the event
262#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
263#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
264pub enum MouseWheel {
265    Horizontal,
266    Vertical,
267    Unknown(usize),
268}
269
270/// Indicates the direction of the mouse wheel spin
271#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
272#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
273pub enum MouseWheelDirection {
274    Forward,
275    Backward,
276    Unknown(u32),
277}
278
279/// The mouse wheel event with information which wheel triggered an event and the direction of the spin
280#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
281#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
282pub struct MouseWheelEvent {
283    pub wheel: MouseWheel,
284    pub direction: Option<MouseWheelDirection>
285
286}
287
288/// Point in per-monitor aware coordinates, see [MSDN](https://learn.microsoft.com/en-us/windows/desktop/api/shellscalingapi/ne-shellscalingapi-process_dpi_awareness)
289#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
290#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
291pub struct Point {
292    pub x: i32,
293    pub y: i32,
294}
295
296/// Holds the new cursor position after mouse move
297#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
298#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
299pub struct MouseMoveEvent {
300    pub point: Option<Point>,
301}
302
303/// Indicates if button was pressed or released
304#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
305#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
306pub enum MouseButtonPress {
307    Down,
308    Up,
309    Other(usize),
310}
311
312/// Indicates if mouse button press is single or double click
313#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
314#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
315pub enum MouseClick {
316    SingleClick,
317    DoubleClick,
318    Other(u32),
319}
320
321/// Identifies which mouse button triggered an event
322#[derive(Copy, Clone, Ord, PartialOrd, Hash, Eq, PartialEq, Debug)]
323#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
324pub enum MouseButton {
325    Left(MouseClick),
326    Right(MouseClick),
327    Middle(MouseClick),
328    /// XBUTTON1
329    X1(MouseClick), 
330    /// XBUTTON2
331    X2(MouseClick), 
332    /// Either XBUTTON1 or XBUTTON2
333    UnkownX(MouseClick),  
334    /// Unexpected mouse button. Raw code stored for reference, see MSDN documentation about low-level hooks.
335    Other(usize),
336}