1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
use std::error::Error; use std::time::SystemTime; use std::{fmt, fmt::Display}; /// Callback type to send to listen function. pub type Callback = fn(event: Event); /// Callback type to send to grab function. pub type GrabCallback = fn(event: Event) -> Option<Event>; /// Errors that occur when trying to capture OS events. /// Be careful on Mac, not setting accessibility does not cause an error /// it justs ignores events. #[derive(Debug)] pub enum ListenError { /// MacOS EventTapError, /// MacOS LoopSourceError, /// Linux MissingDisplayError, /// Linux RecordContextEnablingError, /// Linux RecordContextError, /// Linux XRecordExtensionError, } /// Errors that occur when trying to grab OS events. /// Be careful on Mac, not setting accessibility does not cause an error /// it justs ignores events. #[derive(Debug)] pub enum GrabError { /// MacOS EventTapError, /// MacOS LoopSourceError, /// Linux LinuxNotSupported, } /// Marking an error when we tried to simulate and event #[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 Error for SimulateError {} /// Key names based on physical location on the device /// Merge Option(MacOS) and Alt(Windows, Linux) into Alt /// Merge Windows (Windows), Meta(Linux), Command(MacOS) into Meta /// Characters based on Qwerty layout, don't use this for characters as it WILL /// depend on the layout. Use Event.name instead. Key modifiers gives those keys /// a different value too. /// Careful, on Windows KpReturn does not exist, it' s strictly equivalent to Return, also Keypad keys /// get modified if NumLock is Off and ARE pagedown and so on. #[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub enum Key { /// Alt key on Linux and Windows (option key on macOS) Alt, AltGr, Backspace, CapsLock, ControlLeft, ControlRight, Delete, DownArrow, End, Escape, F1, F10, F11, F12, F2, F3, F4, F5, F6, F7, F8, F9, Home, LeftArrow, /// also known as "windows", "super", and "command" MetaLeft, /// also known as "windows", "super", and "command" 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), } /// Standard mouse buttons /// Some mice have more than 3 buttons. These are not defined, and different /// OSs will give different `Button::Unknown` values. #[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub enum Button { Left, Right, Middle, Unknown(u8), } /// In order to manage different OSs, the current EventType choices are a mix and /// match to account for all possible events. #[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub enum EventType { /// The keys correspond to a standard qwerty layout, they don't correspond /// To the actual letter a user would use, that requires some layout logic to be added. KeyPress(Key), KeyRelease(Key), /// Mouse Button ButtonPress(Button), ButtonRelease(Button), /// Values in pixels. `EventType::MouseMove{x: 0, y: 0}` corresponds to the /// top left corner, with x increasing downward and y increasing rightward MouseMove { x: f64, y: f64, }, /// `delta_y` represents vertical scroll and `delta_x` represents horizontal scroll. /// Positive values correspond to scrolling up or right and negative values /// correspond to scrolling down or left /// Note: Linux does not support horizontal scroll. When simulating scroll on Linux, /// only the sign of delta_y is considered, and not the magnitude to determine wheelup or wheeldown. Wheel { delta_x: i64, delta_y: i64, }, } /// When events arrive from the OS they get some additional information added from /// EventType, which is the time when this event was received, and the name Option /// which contains what characters should be emmitted from that event. This relies /// on the OS layout and keyboard state machinery. /// Caveat: Dead keys don't function on Linux(X11) yet. You will receive None for /// a dead key, and the raw letter instead of accentuated letter. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub struct Event { pub time: SystemTime, pub name: Option<String>, pub event_type: EventType, }