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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
use std::path::PathBuf; use {WindowId, DeviceId, AxisId, ButtonId}; #[derive(Clone, Debug)] pub enum Event { WindowEvent { window_id: WindowId, event: WindowEvent, }, DeviceEvent { device_id: DeviceId, event: DeviceEvent, }, Awakened, } #[derive(Clone, Debug)] pub enum WindowEvent { /// The size of the window has changed. Resized(u32, u32), /// The position of the window has changed. Moved(i32, i32), /// The window has been closed. Closed, /// A file has been dropped into the window. DroppedFile(PathBuf), /// The window received a unicode character. ReceivedCharacter(char), /// The window gained or lost focus. /// /// The parameter is true if the window has gained focus, and false if it has lost focus. Focused(bool), /// An event from the keyboard has been received. KeyboardInput { device_id: DeviceId, input: KeyboardInput }, /// The cursor has moved on the window. /// /// `position` is (x,y) coords in pixels relative to the top-left corner of the window. Because the range of this /// data is limited by the display area and it may have been transformed by the OS to implement effects such as /// mouse acceleration, it should not be used to implement non-cursor-like interactions such as 3D camera control. MouseMoved { device_id: DeviceId, position: (f64, f64) }, /// The cursor has entered the window. MouseEntered { device_id: DeviceId }, /// The cursor has left the window. MouseLeft { device_id: DeviceId }, /// A mouse wheel movement or touchpad scroll occurred. MouseWheel { device_id: DeviceId, delta: MouseScrollDelta, phase: TouchPhase }, /// An mouse button press has been received. MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton }, /// Touchpad pressure event. /// /// At the moment, only supported on Apple forcetouch-capable macbooks. /// The parameters are: pressure level (value between 0 and 1 representing how hard the touchpad /// is being pressed) and stage (integer representing the click level). TouchpadPressure { device_id: DeviceId, pressure: f32, stage: i64 }, /// Motion on some analog axis not otherwise handled. May overlap with mouse motion. AxisMotion { device_id: DeviceId, axis: AxisId, value: f64 }, /// The window needs to be redrawn. Refresh, /// App has been suspended or resumed. /// /// The parameter is true if app was suspended, and false if it has been resumed. Suspended(bool), /// Touch event has been received Touch(Touch) } /// Represents raw hardware events that are not associated with any particular window. /// /// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera or first-person /// game controls. Many physical actions, such as mouse movement, can produce both device and window events. Because /// window events typically arise from virtual devices (corresponding to GUI cursors and keyboard focus) the device IDs /// may not match. /// /// Note that these events are delivered regardless of input focus. #[derive(Clone, Debug)] pub enum DeviceEvent { Added, Removed, Motion { axis: AxisId, value: f64 }, Button { button: ButtonId, state: ElementState }, Key(KeyboardInput), Text { codepoint: char }, } #[derive(Debug, Clone, Copy)] pub struct KeyboardInput { /// Identifies the physical key pressed /// /// This should not change if the user adjusts the host's keyboard map. Use when the physical location of the /// key is more important than the key's host GUI semantics, such as for movement controls in a first-person /// game. pub scancode: ScanCode, pub state: ElementState, /// Identifies the semantic meaning of the key /// /// Use when the semantics of the key are more important than the physical location of the key, such as when /// implementing appropriate behavior for "page up." pub virtual_keycode: Option<VirtualKeyCode>, /// Modifier keys active at the time of this input. /// /// This is tracked internally to avoid tracking errors arising from modifier key state changes when events from /// this device are not being delivered to the application, e.g. due to keyboard focus being elsewhere. pub modifiers: ModifiersState } #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] pub enum TouchPhase { Started, Moved, Ended, Cancelled } #[derive(Debug, Clone, Copy)] /// Represents touch event /// /// Every time user touches screen new Start event with some finger id is generated. /// When the finger is removed from the screen End event with same id is generated. /// /// For every id there will be at least 2 events with phases Start and End (or Cancelled). /// There may be 0 or more Move events. /// /// /// Depending on platform implementation id may or may not be reused by system after End event. /// /// Gesture regonizer using this event should assume that Start event received with same id /// as previously received End event is a new finger and has nothing to do with an old one. /// /// Touch may be cancelled if for example window lost focus. pub struct Touch { pub device_id: DeviceId, pub phase: TouchPhase, pub location: (f64,f64), /// unique identifier of a finger. pub id: u64 } pub type ScanCode = u32; #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] pub enum ElementState { Pressed, Released, } #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] pub enum MouseButton { Left, Right, Middle, Other(u8), } #[derive(Debug, Clone, Copy, PartialEq)] pub enum MouseScrollDelta { /// Amount in lines or rows to scroll in the horizontal /// and vertical directions. /// /// Positive values indicate movement forward /// (away from the user) or rightwards. LineDelta(f32, f32), /// Amount in pixels to scroll in the horizontal and /// vertical direction. /// /// Scroll events are expressed as a PixelDelta if /// supported by the device (eg. a touchpad) and /// platform. PixelDelta(f32, f32) } #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] #[repr(u32)] pub enum VirtualKeyCode { /// The '1' key over the letters. Key1, /// The '2' key over the letters. Key2, /// The '3' key over the letters. Key3, /// The '4' key over the letters. Key4, /// The '5' key over the letters. Key5, /// The '6' key over the letters. Key6, /// The '7' key over the letters. Key7, /// The '8' key over the letters. Key8, /// The '9' key over the letters. Key9, /// The '0' key over the 'O' and 'P' keys. Key0, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, /// The Escape key, next to F1. Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, /// Print Screen/SysRq. Snapshot, /// Scroll Lock. Scroll, /// Pause/Break key, next to Scroll lock. Pause, /// `Insert`, next to Backspace. Insert, Home, Delete, End, PageDown, PageUp, Left, Up, Right, Down, /// The Backspace key, right over Enter. // TODO: rename Back, /// The Enter key. Return, /// The space bar. Space, /// The "Compose" key on Linux. Compose, Numlock, Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, AbntC1, AbntC2, Add, Apostrophe, Apps, At, Ax, Backslash, Calculator, Capital, Colon, Comma, Convert, Decimal, Divide, Equals, Grave, Kana, Kanji, LAlt, LBracket, LControl, LMenu, LShift, LWin, Mail, MediaSelect, MediaStop, Minus, Multiply, Mute, MyComputer, NavigateForward, // also called "Prior" NavigateBackward, // also called "Next" NextTrack, NoConvert, NumpadComma, NumpadEnter, NumpadEquals, OEM102, Period, PlayPause, Power, PrevTrack, RAlt, RBracket, RControl, RMenu, RShift, RWin, Semicolon, Slash, Sleep, Stop, Subtract, Sysrq, Tab, Underline, Unlabeled, VolumeDown, VolumeUp, Wake, WebBack, WebFavorites, WebForward, WebHome, WebRefresh, WebSearch, WebStop, Yen, } /// Represents the current state of the keyboard modifiers /// /// Each field of this struct represents a modifier and is `true` if this modifier is active. #[derive(Default, Debug, Clone, Copy)] pub struct ModifiersState { /// The "shift" key pub shift: bool, /// The "control" key pub ctrl: bool, /// The "alt" key pub alt: bool, /// The "logo" key /// /// This is the "windows" key on PC and "command" key on Mac. pub logo: bool }