use crate::keycode::Key;
use std::time::SystemTime;
#[cfg(feature = "recorder")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub enum EventType {
HookEnabled,
HookDisabled,
KeyPressed,
KeyReleased,
KeyTyped,
MousePressed,
MouseReleased,
MouseClicked,
MouseMoved,
MouseDragged,
MouseWheel,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub enum Button {
Left,
Right,
Middle,
Button4,
Button5,
Unknown(u8),
}
impl Button {
pub fn number(&self) -> u8 {
match self {
Button::Left => 1,
Button::Right => 2,
Button::Middle => 3,
Button::Button4 => 4,
Button::Button5 => 5,
Button::Unknown(n) => *n,
}
}
pub fn from_number(n: u8) -> Self {
match n {
1 => Button::Left,
2 => Button::Right,
3 => Button::Middle,
4 => Button::Button4,
5 => Button::Button5,
_ => Button::Unknown(n),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub enum ScrollDirection {
Up,
Down,
Left,
Right,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub struct KeyboardData {
pub key: Key,
pub raw_code: u32,
pub char: Option<char>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub struct MouseData {
pub button: Option<Button>,
pub x: f64,
pub y: f64,
pub clicks: u8,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub struct WheelData {
pub x: f64,
pub y: f64,
pub direction: ScrollDirection,
pub delta: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "recorder", derive(Serialize, Deserialize))]
pub struct Event {
pub event_type: EventType,
pub time: SystemTime,
pub mask: u32,
pub keyboard: Option<KeyboardData>,
pub mouse: Option<MouseData>,
pub wheel: Option<WheelData>,
}
impl Event {
pub fn new(event_type: EventType) -> Self {
Self {
event_type,
time: SystemTime::now(),
mask: crate::state::get_mask(),
keyboard: None,
mouse: None,
wheel: None,
}
}
pub fn hook_enabled() -> Self {
Self::new(EventType::HookEnabled)
}
pub fn hook_disabled() -> Self {
Self::new(EventType::HookDisabled)
}
pub fn key_pressed(key: Key, raw_code: u32) -> Self {
let mut event = Self::new(EventType::KeyPressed);
event.keyboard = Some(KeyboardData {
key,
raw_code,
char: None,
});
event
}
pub fn key_released(key: Key, raw_code: u32) -> Self {
let mut event = Self::new(EventType::KeyReleased);
event.keyboard = Some(KeyboardData {
key,
raw_code,
char: None,
});
event
}
pub fn key_typed(key: Key, raw_code: u32, char: char) -> Self {
let mut event = Self::new(EventType::KeyTyped);
event.keyboard = Some(KeyboardData {
key,
raw_code,
char: Some(char),
});
event
}
pub fn mouse_pressed(button: Button, x: f64, y: f64) -> Self {
let mut event = Self::new(EventType::MousePressed);
event.mouse = Some(MouseData {
button: Some(button),
x,
y,
clicks: 0,
});
event
}
pub fn mouse_released(button: Button, x: f64, y: f64) -> Self {
let mut event = Self::new(EventType::MouseReleased);
event.mouse = Some(MouseData {
button: Some(button),
x,
y,
clicks: 0,
});
event
}
pub fn mouse_clicked(button: Button, x: f64, y: f64, clicks: u8) -> Self {
let mut event = Self::new(EventType::MouseClicked);
event.mouse = Some(MouseData {
button: Some(button),
x,
y,
clicks,
});
event
}
pub fn mouse_moved(x: f64, y: f64) -> Self {
let mut event = Self::new(EventType::MouseMoved);
event.mouse = Some(MouseData {
button: None,
x,
y,
clicks: 0,
});
event
}
pub fn mouse_dragged(x: f64, y: f64) -> Self {
let mut event = Self::new(EventType::MouseDragged);
event.mouse = Some(MouseData {
button: None,
x,
y,
clicks: 0,
});
event
}
pub fn mouse_wheel(x: f64, y: f64, direction: ScrollDirection, delta: f64) -> Self {
let mut event = Self::new(EventType::MouseWheel);
event.wheel = Some(WheelData {
x,
y,
direction,
delta,
});
event
}
pub fn is_keyboard(&self) -> bool {
matches!(
self.event_type,
EventType::KeyPressed | EventType::KeyReleased | EventType::KeyTyped
)
}
pub fn is_mouse(&self) -> bool {
matches!(
self.event_type,
EventType::MousePressed
| EventType::MouseReleased
| EventType::MouseClicked
| EventType::MouseMoved
| EventType::MouseDragged
| EventType::MouseWheel
)
}
}