#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::time::SystemTime;
use std::{fmt, fmt::Display};
pub type GrabCallback = fn(event: Event) -> Option<Event>;
#[derive(Debug)]
#[non_exhaustive]
pub enum ListenError {
EventTapError,
LoopSourceError,
MissingDisplayError,
KeyboardError,
RecordContextEnablingError,
RecordContextError,
XRecordExtensionError,
KeyHookError(u32),
MouseHookError(u32),
}
#[derive(Debug)]
#[non_exhaustive]
pub enum GrabError {
EventTapError,
LoopSourceError,
MissingDisplayError,
KeyboardError,
KeyHookError(u32),
MouseHookError(u32),
SimulateError,
IoError(std::io::Error),
}
#[non_exhaustive]
#[derive(Debug)]
pub enum DisplayError {
NoDisplay,
ConversionError,
}
impl From<SimulateError> for GrabError {
fn from(_: SimulateError) -> GrabError {
GrabError::SimulateError
}
}
impl From<std::io::Error> for GrabError {
fn from(err: std::io::Error) -> GrabError {
GrabError::IoError(err)
}
}
#[derive(Debug)]
pub struct SimulateError;
impl Display for SimulateError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Could not simulate event")
}
}
impl std::error::Error for SimulateError {}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum Key {
Alt,
AltGr,
Backspace,
CapsLock,
ControlLeft,
ControlRight,
Delete,
DownArrow,
End,
Escape,
F1,
F10,
F11,
F12,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
Home,
LeftArrow,
MetaLeft,
MetaRight,
PageDown,
PageUp,
Return,
RightArrow,
ShiftLeft,
ShiftRight,
Space,
Tab,
UpArrow,
PrintScreen,
ScrollLock,
Pause,
NumLock,
BackQuote,
Num1,
Num2,
Num3,
Num4,
Num5,
Num6,
Num7,
Num8,
Num9,
Num0,
Minus,
Equal,
KeyQ,
KeyW,
KeyE,
KeyR,
KeyT,
KeyY,
KeyU,
KeyI,
KeyO,
KeyP,
LeftBracket,
RightBracket,
KeyA,
KeyS,
KeyD,
KeyF,
KeyG,
KeyH,
KeyJ,
KeyK,
KeyL,
SemiColon,
Quote,
BackSlash,
IntlBackslash,
KeyZ,
KeyX,
KeyC,
KeyV,
KeyB,
KeyN,
KeyM,
Comma,
Dot,
Slash,
Insert,
KpReturn,
KpMinus,
KpPlus,
KpMultiply,
KpDivide,
Kp0,
Kp1,
Kp2,
Kp3,
Kp4,
Kp5,
Kp6,
Kp7,
Kp8,
Kp9,
KpDelete,
Function,
Unknown(u32),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum Button {
Left,
Right,
Middle,
Unknown(u8),
}
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum EventType {
KeyPress(Key),
KeyRelease(Key),
ButtonPress(Button),
ButtonRelease(Button),
MouseMove {
x: f64,
y: f64,
},
Wheel {
delta_x: i64,
delta_y: i64,
},
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Event {
pub time: SystemTime,
pub name: Option<String>,
pub event_type: EventType,
}
pub trait KeyboardState {
fn add(&mut self, event_type: &EventType) -> Option<String>;
fn reset(&mut self);
}