winit/event.rs
1//! The event enums and assorted supporting types.
2//!
3//! These are sent to the closure given to [`EventLoop::run_app(...)`], where they get
4//! processed and used to modify the program state. For more details, see the root-level
5//! documentation.
6//!
7//! Some of these events represent different "parts" of a traditional event-handling loop. You could
8//! approximate the basic ordering loop of [`EventLoop::run_app(...)`] like this:
9//!
10//! ```rust,ignore
11//! let mut start_cause = StartCause::Init;
12//!
13//! while !elwt.exiting() {
14//! app.new_events(event_loop, start_cause);
15//!
16//! for event in (window events, user events, device events) {
17//! // This will pick the right method on the application based on the event.
18//! app.handle_event(event_loop, event);
19//! }
20//!
21//! for window_id in (redraw windows) {
22//! app.window_event(event_loop, window_id, RedrawRequested);
23//! }
24//!
25//! app.about_to_wait(event_loop);
26//! start_cause = wait_if_necessary();
27//! }
28//!
29//! app.exiting(event_loop);
30//! ```
31//!
32//! This leaves out timing details like [`ControlFlow::WaitUntil`] but hopefully
33//! describes what happens in what order.
34//!
35//! [`EventLoop::run_app(...)`]: crate::event_loop::EventLoop::run_app
36//! [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
37use std::path::PathBuf;
38use std::sync::{Mutex, Weak};
39#[cfg(not(web_platform))]
40use std::time::Instant;
41
42#[cfg(feature = "serde")]
43use serde::{Deserialize, Serialize};
44use smol_str::SmolStr;
45#[cfg(web_platform)]
46use web_time::Instant;
47
48use crate::dpi::{PhysicalPosition, PhysicalSize};
49use crate::error::RequestError;
50use crate::event_loop::AsyncRequestSerial;
51use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState};
52use crate::platform_impl;
53#[cfg(doc)]
54use crate::window::Window;
55use crate::window::{ActivationToken, Theme, WindowId};
56
57// TODO: Remove once the backends can call `ApplicationHandler` methods directly. For now backends
58// like Windows and Web require `Event` to wire user events, otherwise each backend will have to
59// wrap `Event` in some other structure.
60/// Describes a generic event.
61///
62/// See the module-level docs for more information on the event loop manages each event.
63#[allow(dead_code)]
64#[derive(Debug, Clone, PartialEq)]
65pub enum Event {
66 /// See [`ApplicationHandler::new_events`] for details.
67 ///
68 /// [`ApplicationHandler::new_events`]: crate::application::ApplicationHandler::new_events
69 NewEvents(StartCause),
70
71 /// See [`ApplicationHandler::window_event`] for details.
72 ///
73 /// [`ApplicationHandler::window_event`]: crate::application::ApplicationHandler::window_event
74 #[allow(clippy::enum_variant_names)]
75 WindowEvent { window_id: WindowId, event: WindowEvent },
76
77 /// See [`ApplicationHandler::device_event`] for details.
78 ///
79 /// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event
80 #[allow(clippy::enum_variant_names)]
81 DeviceEvent { device_id: DeviceId, event: DeviceEvent },
82
83 /// See [`ApplicationHandler::suspended`] for details.
84 ///
85 /// [`ApplicationHandler::suspended`]: crate::application::ApplicationHandler::suspended
86 Suspended,
87
88 /// See [`ApplicationHandler::can_create_surfaces`] for details.
89 ///
90 /// [`ApplicationHandler::can_create_surfaces`]: crate::application::ApplicationHandler::can_create_surfaces
91 CreateSurfaces,
92
93 /// See [`ApplicationHandler::resumed`] for details.
94 ///
95 /// [`ApplicationHandler::resumed`]: crate::application::ApplicationHandler::resumed
96 Resumed,
97
98 /// See [`ApplicationHandler::about_to_wait`] for details.
99 ///
100 /// [`ApplicationHandler::about_to_wait`]: crate::application::ApplicationHandler::about_to_wait
101 AboutToWait,
102
103 /// See [`ApplicationHandler::exiting`] for details.
104 ///
105 /// [`ApplicationHandler::exiting`]: crate::application::ApplicationHandler::exiting
106 LoopExiting,
107
108 /// See [`ApplicationHandler::memory_warning`] for details.
109 ///
110 /// [`ApplicationHandler::memory_warning`]: crate::application::ApplicationHandler::memory_warning
111 MemoryWarning,
112
113 /// User requested a wake up.
114 UserWakeUp,
115}
116
117/// Describes the reason the event loop is resuming.
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
119pub enum StartCause {
120 /// Sent if the time specified by [`ControlFlow::WaitUntil`] has been reached. Contains the
121 /// moment the timeout was requested and the requested resume time. The actual resume time is
122 /// guaranteed to be equal to or after the requested resume time.
123 ///
124 /// [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
125 ResumeTimeReached { start: Instant, requested_resume: Instant },
126
127 /// Sent if the OS has new events to send to the window, after a wait was requested. Contains
128 /// the moment the wait was requested and the resume time, if requested.
129 WaitCancelled { start: Instant, requested_resume: Option<Instant> },
130
131 /// Sent if the event loop is being resumed after the loop's control flow was set to
132 /// [`ControlFlow::Poll`].
133 ///
134 /// [`ControlFlow::Poll`]: crate::event_loop::ControlFlow::Poll
135 Poll,
136
137 /// Sent once, immediately after `run` is called. Indicates that the loop was just initialized.
138 Init,
139}
140
141/// Describes an event from a [`Window`].
142#[derive(Debug, Clone, PartialEq)]
143pub enum WindowEvent {
144 /// The activation token was delivered back and now could be used.
145 #[cfg_attr(not(any(x11_platform, wayland_platform)), allow(rustdoc::broken_intra_doc_links))]
146 /// Delivered in response to [`request_activation_token`].
147 ///
148 /// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token
149 ActivationTokenDone { serial: AsyncRequestSerial, token: ActivationToken },
150
151 /// The size of the window's surface has changed.
152 ///
153 /// Contains the new dimensions of the surface (can also be retrieved with
154 /// [`Window::surface_size`]).
155 ///
156 /// [`Window::surface_size`]: crate::window::Window::surface_size
157 SurfaceResized(PhysicalSize<u32>),
158
159 /// The suggested bounds of the window's surface has changed.
160 ///
161 /// Contains the new bounds of the surface
162 ///
163 /// - **iOS / Android / Web / Orbital / Windows:** Unsupported.
164 SuggestedBounds(Option<PhysicalSize<u32>>),
165
166 /// The position of the window has changed. Contains the window's new position.
167 ///
168 /// ## Platform-specific
169 ///
170 /// - **iOS / Android / Web / Wayland:** Unsupported.
171 Moved(PhysicalPosition<i32>),
172
173 /// The window has been requested to close.
174 CloseRequested,
175
176 /// The window has been destroyed.
177 Destroyed,
178
179 /// A file has been dropped into the window.
180 ///
181 /// When the user drops multiple files at once, this event will be emitted for each file
182 /// separately.
183 DroppedFile(PathBuf),
184
185 /// A file is being hovered over the window.
186 ///
187 /// When the user hovers multiple files at once, this event will be emitted for each file
188 /// separately.
189 HoveredFile(PathBuf),
190
191 /// A file was hovered, but has exited the window.
192 ///
193 /// There will be a single `HoveredFileCancelled` event triggered even if multiple files were
194 /// hovered.
195 HoveredFileCancelled,
196
197 /// The window gained or lost focus.
198 ///
199 /// The parameter is true if the window has gained focus, and false if it has lost focus.
200 Focused(bool),
201
202 /// An event from the keyboard has been received.
203 ///
204 /// ## Platform-specific
205 /// - **Windows:** The shift key overrides NumLock. In other words, while shift is held down,
206 /// numpad keys act as if NumLock wasn't active. When this is used, the OS sends fake key
207 /// events which are not marked as `is_synthetic`.
208 KeyboardInput {
209 device_id: DeviceId,
210 event: KeyEvent,
211
212 /// If `true`, the event was generated synthetically by winit
213 /// in one of the following circumstances:
214 ///
215 /// * Synthetic key press events are generated for all keys pressed when a window gains
216 /// focus. Likewise, synthetic key release events are generated for all keys pressed when
217 /// a window goes out of focus. ***Currently, this is only functional on X11 and
218 /// Windows***
219 ///
220 /// Otherwise, this value is always `false`.
221 is_synthetic: bool,
222 },
223
224 /// The keyboard modifiers have changed.
225 ModifiersChanged(Modifiers),
226
227 /// An event from an input method.
228 ///
229 /// **Note:** You have to explicitly enable this event using [`Window::set_ime_allowed`].
230 ///
231 /// ## Platform-specific
232 ///
233 /// - **iOS / Android / Web / Orbital:** Unsupported.
234 Ime(Ime),
235
236 /// The cursor has moved on the window.
237 ///
238 /// ## Platform-specific
239 ///
240 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
241 ///
242 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
243 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
244 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
245 CursorMoved {
246 device_id: DeviceId,
247
248 /// (x,y) coords in pixels relative to the top-left corner of the window. Because the range
249 /// of this data is limited by the display area and it may have been transformed by
250 /// the OS to implement effects such as cursor acceleration, it should not be used
251 /// to implement non-cursor-like interactions such as 3D camera control. For that,
252 /// consider [`DeviceEvent::MouseMotion`].
253 position: PhysicalPosition<f64>,
254 },
255
256 /// The cursor has entered the window.
257 ///
258 /// ## Platform-specific
259 ///
260 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
261 ///
262 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
263 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
264 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
265 CursorEntered { device_id: DeviceId },
266
267 /// The cursor has left the window.
268 ///
269 /// ## Platform-specific
270 ///
271 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
272 ///
273 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
274 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
275 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
276 CursorLeft { device_id: DeviceId },
277
278 /// A mouse wheel movement or touchpad scroll occurred.
279 MouseWheel { device_id: DeviceId, delta: MouseScrollDelta, phase: TouchPhase },
280
281 /// An mouse button press has been received.
282 MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton },
283
284 /// Two-finger pinch gesture, often used for magnification.
285 ///
286 /// ## Platform-specific
287 ///
288 /// - Only available on **macOS** and **iOS**.
289 /// - On iOS, not recognized by default. It must be enabled when needed.
290 PinchGesture {
291 device_id: DeviceId,
292 /// Positive values indicate magnification (zooming in) and negative
293 /// values indicate shrinking (zooming out).
294 ///
295 /// This value may be NaN.
296 delta: f64,
297 phase: TouchPhase,
298 },
299
300 /// N-finger pan gesture
301 ///
302 /// ## Platform-specific
303 ///
304 /// - Only available on **iOS**.
305 /// - On iOS, not recognized by default. It must be enabled when needed.
306 PanGesture {
307 device_id: DeviceId,
308 /// Change in pixels of pan gesture from last update.
309 delta: PhysicalPosition<f32>,
310 phase: TouchPhase,
311 },
312
313 /// Double tap gesture.
314 ///
315 /// On a Mac, smart magnification is triggered by a double tap with two fingers
316 /// on the trackpad and is commonly used to zoom on a certain object
317 /// (e.g. a paragraph of a PDF) or (sort of like a toggle) to reset any zoom.
318 /// The gesture is also supported in Safari, Pages, etc.
319 ///
320 /// The event is general enough that its generating gesture is allowed to vary
321 /// across platforms. It could also be generated by another device.
322 ///
323 /// Unfortunately, neither [Windows](https://support.microsoft.com/en-us/windows/touch-gestures-for-windows-a9d28305-4818-a5df-4e2b-e5590f850741)
324 /// nor [Wayland](https://wayland.freedesktop.org/libinput/doc/latest/gestures.html)
325 /// support this gesture or any other gesture with the same effect.
326 ///
327 /// ## Platform-specific
328 ///
329 /// - Only available on **macOS 10.8** and later, and **iOS**.
330 /// - On iOS, not recognized by default. It must be enabled when needed.
331 DoubleTapGesture { device_id: DeviceId },
332
333 /// Two-finger rotation gesture.
334 ///
335 /// Positive delta values indicate rotation counterclockwise and
336 /// negative delta values indicate rotation clockwise.
337 ///
338 /// ## Platform-specific
339 ///
340 /// - Only available on **macOS** and **iOS**.
341 /// - On iOS, not recognized by default. It must be enabled when needed.
342 RotationGesture {
343 device_id: DeviceId,
344 /// change in rotation in degrees
345 delta: f32,
346 phase: TouchPhase,
347 },
348
349 /// Touchpad pressure event.
350 ///
351 /// At the moment, only supported on Apple forcetouch-capable macbooks.
352 /// The parameters are: pressure level (value between 0 and 1 representing how hard the
353 /// touchpad is being pressed) and stage (integer representing the click level).
354 TouchpadPressure { device_id: DeviceId, pressure: f32, stage: i64 },
355
356 /// Touch event has been received
357 ///
358 /// ## Platform-specific
359 ///
360 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
361 /// - **macOS:** Unsupported.
362 ///
363 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
364 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
365 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
366 Touch(Touch),
367
368 /// The window's scale factor has changed.
369 ///
370 /// The following user actions can cause DPI changes:
371 ///
372 /// * Changing the display's resolution.
373 /// * Changing the display's scale factor (e.g. in Control Panel on Windows).
374 /// * Moving the window to a display with a different scale factor.
375 ///
376 /// To update the window size, use the provided [`SurfaceSizeWriter`] handle. By default, the
377 /// window is resized to the value suggested by the OS, but it can be changed to any value.
378 ///
379 /// For more information about DPI in general, see the [`dpi`] crate.
380 ScaleFactorChanged {
381 scale_factor: f64,
382 /// Handle to update surface size during scale changes.
383 ///
384 /// See [`SurfaceSizeWriter`] docs for more details.
385 surface_size_writer: SurfaceSizeWriter,
386 },
387
388 /// The system window theme has changed.
389 ///
390 /// Applications might wish to react to this to change the theme of the content of the window
391 /// when the system changes the window theme.
392 ///
393 /// This only reports a change if the window theme was not overridden by [`Window::set_theme`].
394 ///
395 /// ## Platform-specific
396 ///
397 /// - **iOS / Android / X11 / Wayland / Orbital:** Unsupported.
398 ThemeChanged(Theme),
399
400 /// The window has been occluded (completely hidden from view).
401 ///
402 /// This is different to window visibility as it depends on whether the window is closed,
403 /// minimised, set invisible, or fully occluded by another window.
404 ///
405 /// ## Platform-specific
406 ///
407 /// ### iOS
408 ///
409 /// On iOS, the `Occluded(false)` event is emitted in response to an
410 /// [`applicationWillEnterForeground`] callback which means the application should start
411 /// preparing its data. The `Occluded(true)` event is emitted in response to an
412 /// [`applicationDidEnterBackground`] callback which means the application should free
413 /// resources (according to the [iOS application lifecycle]).
414 ///
415 /// [`applicationWillEnterForeground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623076-applicationwillenterforeground
416 /// [`applicationDidEnterBackground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground
417 /// [iOS application lifecycle]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle
418 ///
419 /// ### Others
420 ///
421 /// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
422 /// - **Android / Wayland / Windows / Orbital:** Unsupported.
423 ///
424 /// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
425 /// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
426 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
427 Occluded(bool),
428
429 /// Emitted when a window should be redrawn.
430 ///
431 /// This gets triggered in two scenarios:
432 /// - The OS has performed an operation that's invalidated the window's contents (such as
433 /// resizing the window).
434 /// - The application has explicitly requested a redraw via [`Window::request_redraw`].
435 ///
436 /// Winit will aggregate duplicate redraw requests into a single event, to
437 /// help avoid duplicating rendering work.
438 RedrawRequested,
439}
440
441/// Identifier of an input device.
442///
443/// Whenever you receive an event arising from a particular input device, this event contains a
444/// `DeviceId` which identifies its origin. Note that devices may be virtual (representing an
445/// on-screen cursor and keyboard focus) or physical. Virtual devices typically aggregate inputs
446/// from multiple physical devices.
447#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
448pub struct DeviceId(pub(crate) platform_impl::DeviceId);
449
450impl Default for DeviceId {
451 fn default() -> Self {
452 Self::dummy()
453 }
454}
455
456impl DeviceId {
457 /// Returns a dummy id, useful for unit testing.
458 ///
459 /// # Notes
460 ///
461 /// The only guarantee made about the return value of this function is that
462 /// it will always be equal to itself and to future values returned by this function.
463 /// No other guarantees are made. This may be equal to a real `DeviceId`.
464 pub const fn dummy() -> Self {
465 DeviceId(platform_impl::DeviceId::dummy())
466 }
467}
468
469/// Identifier of a finger in a touch event.
470///
471/// Whenever a touch event is received it contains a `FingerId` which uniquely identifies the finger
472/// used for the current interaction.
473#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
474pub struct FingerId(pub(crate) platform_impl::FingerId);
475
476impl FingerId {
477 /// Returns a dummy id, useful for unit testing.
478 ///
479 /// # Notes
480 ///
481 /// The only guarantee made about the return value of this function is that
482 /// it will always be equal to itself and to future values returned by this function.
483 /// No other guarantees are made. This may be equal to a real `FingerId`.
484 pub const fn dummy() -> Self {
485 FingerId(platform_impl::FingerId::dummy())
486 }
487}
488
489/// Represents raw hardware events that are not associated with any particular window.
490///
491/// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera
492/// or first-person game controls. Many physical actions, such as mouse movement, can produce both
493/// device and window events. Because window events typically arise from virtual devices
494/// (corresponding to GUI cursors and keyboard focus) the device IDs may not match.
495///
496/// Note that these events are delivered regardless of input focus.
497#[derive(Clone, Copy, Debug, PartialEq)]
498pub enum DeviceEvent {
499 /// Change in physical position of a pointing device.
500 ///
501 /// This represents raw, unfiltered physical motion. Not to be confused with
502 /// [`WindowEvent::CursorMoved`].
503 ///
504 /// ## Platform-specific
505 ///
506 /// **Web:** Only returns raw data, not OS accelerated, if [`CursorGrabMode::Locked`] is used
507 /// and browser support is available, see
508 #[cfg_attr(
509 any(web_platform, docsrs),
510 doc = "[`ActiveEventLoopExtWeb::is_cursor_lock_raw()`][crate::platform::web::ActiveEventLoopExtWeb::is_cursor_lock_raw()]."
511 )]
512 #[cfg_attr(
513 not(any(web_platform, docsrs)),
514 doc = "`ActiveEventLoopExtWeb::is_cursor_lock_raw()`."
515 )]
516 ///
517 #[rustfmt::skip]
518 /// [`CursorGrabMode::Locked`]: crate::window::CursorGrabMode::Locked
519 MouseMotion {
520 /// (x, y) change in position in unspecified units.
521 ///
522 /// Different devices may use different units.
523 delta: (f64, f64),
524 },
525
526 /// Physical scroll event
527 MouseWheel {
528 delta: MouseScrollDelta,
529 },
530
531 Button {
532 button: ButtonId,
533 state: ElementState,
534 },
535
536 Key(RawKeyEvent),
537}
538
539/// Describes a keyboard input as a raw device event.
540///
541/// Note that holding down a key may produce repeated `RawKeyEvent`s. The
542/// operating system doesn't provide information whether such an event is a
543/// repeat or the initial keypress. An application may emulate this by, for
544/// example keeping a Map/Set of pressed keys and determining whether a keypress
545/// corresponds to an already pressed key.
546#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
547#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
548pub struct RawKeyEvent {
549 pub physical_key: keyboard::PhysicalKey,
550 pub state: ElementState,
551}
552
553/// Describes a keyboard input targeting a window.
554#[derive(Debug, Clone, Eq, PartialEq, Hash)]
555pub struct KeyEvent {
556 /// Represents the position of a key independent of the currently active layout.
557 ///
558 /// It also uniquely identifies the physical key (i.e. it's mostly synonymous with a scancode).
559 /// The most prevalent use case for this is games. For example the default keys for the player
560 /// to move around might be the W, A, S, and D keys on a US layout. The position of these keys
561 /// is more important than their label, so they should map to Z, Q, S, and D on an "AZERTY"
562 /// layout. (This value is `KeyCode::KeyW` for the Z key on an AZERTY layout.)
563 ///
564 /// ## Caveats
565 ///
566 /// - Certain niche hardware will shuffle around physical key positions, e.g. a keyboard that
567 /// implements DVORAK in hardware (or firmware)
568 /// - Your application will likely have to handle keyboards which are missing keys that your
569 /// own keyboard has.
570 /// - Certain `KeyCode`s will move between a couple of different positions depending on what
571 /// layout the keyboard was manufactured to support.
572 ///
573 /// **Because of these caveats, it is important that you provide users with a way to configure
574 /// most (if not all) keybinds in your application.**
575 ///
576 /// ## `Fn` and `FnLock`
577 ///
578 /// `Fn` and `FnLock` key events are *exceedingly unlikely* to be emitted by Winit. These keys
579 /// are usually handled at the hardware or OS level, and aren't surfaced to applications. If
580 /// you somehow see this in the wild, we'd like to know :)
581 pub physical_key: keyboard::PhysicalKey,
582
583 // Allowing `broken_intra_doc_links` for `logical_key`, because
584 // `key_without_modifiers` is not available on all platforms
585 #[cfg_attr(
586 not(any(windows_platform, macos_platform, x11_platform, wayland_platform)),
587 allow(rustdoc::broken_intra_doc_links)
588 )]
589 /// This value is affected by all modifiers except <kbd>Ctrl</kbd>.
590 ///
591 /// This has two use cases:
592 /// - Allows querying whether the current input is a Dead key.
593 /// - Allows handling key-bindings on platforms which don't support [`key_without_modifiers`].
594 ///
595 /// If you use this field (or [`key_without_modifiers`] for that matter) for keyboard
596 /// shortcuts, **it is important that you provide users with a way to configure your
597 /// application's shortcuts so you don't render your application unusable for users with an
598 /// incompatible keyboard layout.**
599 ///
600 /// ## Platform-specific
601 /// - **Web:** Dead keys might be reported as the real key instead of `Dead` depending on the
602 /// browser/OS.
603 ///
604 /// [`key_without_modifiers`]: crate::platform::modifier_supplement::KeyEventExtModifierSupplement::key_without_modifiers
605 pub logical_key: keyboard::Key,
606
607 /// Contains the text produced by this keypress.
608 ///
609 /// In most cases this is identical to the content
610 /// of the `Character` variant of `logical_key`.
611 /// However, on Windows when a dead key was pressed earlier
612 /// but cannot be combined with the character from this
613 /// keypress, the produced text will consist of two characters:
614 /// the dead-key-character followed by the character resulting
615 /// from this keypress.
616 ///
617 /// An additional difference from `logical_key` is that
618 /// this field stores the text representation of any key
619 /// that has such a representation. For example when
620 /// `logical_key` is `Key::Named(NamedKey::Enter)`, this field is `Some("\r")`.
621 ///
622 /// This is `None` if the current keypress cannot
623 /// be interpreted as text.
624 ///
625 /// See also: `text_with_all_modifiers()`
626 pub text: Option<SmolStr>,
627
628 /// Contains the location of this key on the keyboard.
629 ///
630 /// Certain keys on the keyboard may appear in more than once place. For example, the "Shift"
631 /// key appears on the left side of the QWERTY keyboard as well as the right side. However,
632 /// both keys have the same symbolic value. Another example of this phenomenon is the "1"
633 /// key, which appears both above the "Q" key and as the "Keypad 1" key.
634 ///
635 /// This field allows the user to differentiate between keys like this that have the same
636 /// symbolic value but different locations on the keyboard.
637 ///
638 /// See the [`KeyLocation`] type for more details.
639 ///
640 /// [`KeyLocation`]: crate::keyboard::KeyLocation
641 pub location: keyboard::KeyLocation,
642
643 /// Whether the key is being pressed or released.
644 ///
645 /// See the [`ElementState`] type for more details.
646 pub state: ElementState,
647
648 /// Whether or not this key is a key repeat event.
649 ///
650 /// On some systems, holding down a key for some period of time causes that key to be repeated
651 /// as though it were being pressed and released repeatedly. This field is `true` if and only
652 /// if this event is the result of one of those repeats.
653 ///
654 /// # Example
655 ///
656 /// In games, you often want to ignore repated key events - this can be
657 /// done by ignoring events where this property is set.
658 ///
659 /// ```no_run
660 /// use winit::event::{ElementState, KeyEvent, WindowEvent};
661 /// use winit::keyboard::{KeyCode, PhysicalKey};
662 /// # let window_event = WindowEvent::RedrawRequested; // To make the example compile
663 /// match window_event {
664 /// WindowEvent::KeyboardInput {
665 /// event:
666 /// KeyEvent {
667 /// physical_key: PhysicalKey::Code(KeyCode::KeyW),
668 /// state: ElementState::Pressed,
669 /// repeat: false,
670 /// ..
671 /// },
672 /// ..
673 /// } => {
674 /// // The physical key `W` was pressed, and it was not a repeat
675 /// },
676 /// _ => {}, // Handle other events
677 /// }
678 /// ```
679 pub repeat: bool,
680
681 /// Platform-specific key event information.
682 ///
683 /// On Windows, Linux and macOS, this type contains the key without modifiers and the text with
684 /// all modifiers applied.
685 ///
686 /// On Android, iOS, Redox and Web, this type is a no-op.
687 pub(crate) platform_specific: platform_impl::KeyEventExtra,
688}
689
690/// Describes keyboard modifiers event.
691#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
692#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
693pub struct Modifiers {
694 pub(crate) state: ModifiersState,
695
696 // NOTE: Currently pressed modifiers keys.
697 //
698 // The field providing a metadata, it shouldn't be used as a source of truth.
699 pub(crate) pressed_mods: ModifiersKeys,
700}
701
702impl Modifiers {
703 /// The state of the modifiers.
704 pub fn state(&self) -> ModifiersState {
705 self.state
706 }
707
708 /// The state of the left shift key.
709 pub fn lshift_state(&self) -> ModifiersKeyState {
710 self.mod_state(ModifiersKeys::LSHIFT)
711 }
712
713 /// The state of the right shift key.
714 pub fn rshift_state(&self) -> ModifiersKeyState {
715 self.mod_state(ModifiersKeys::RSHIFT)
716 }
717
718 /// The state of the left alt key.
719 pub fn lalt_state(&self) -> ModifiersKeyState {
720 self.mod_state(ModifiersKeys::LALT)
721 }
722
723 /// The state of the right alt key.
724 pub fn ralt_state(&self) -> ModifiersKeyState {
725 self.mod_state(ModifiersKeys::RALT)
726 }
727
728 /// The state of the left control key.
729 pub fn lcontrol_state(&self) -> ModifiersKeyState {
730 self.mod_state(ModifiersKeys::LCONTROL)
731 }
732
733 /// The state of the right control key.
734 pub fn rcontrol_state(&self) -> ModifiersKeyState {
735 self.mod_state(ModifiersKeys::RCONTROL)
736 }
737
738 /// The state of the left super key.
739 pub fn lsuper_state(&self) -> ModifiersKeyState {
740 self.mod_state(ModifiersKeys::LSUPER)
741 }
742
743 /// The state of the right super key.
744 pub fn rsuper_state(&self) -> ModifiersKeyState {
745 self.mod_state(ModifiersKeys::RSUPER)
746 }
747
748 fn mod_state(&self, modifier: ModifiersKeys) -> ModifiersKeyState {
749 if self.pressed_mods.contains(modifier) {
750 ModifiersKeyState::Pressed
751 } else {
752 ModifiersKeyState::Unknown
753 }
754 }
755}
756
757impl From<ModifiersState> for Modifiers {
758 fn from(value: ModifiersState) -> Self {
759 Self { state: value, pressed_mods: Default::default() }
760 }
761}
762
763/// Describes [input method](https://en.wikipedia.org/wiki/Input_method) events.
764///
765/// This is also called a "composition event".
766///
767/// Most keypresses using a latin-like keyboard layout simply generate a
768/// [`WindowEvent::KeyboardInput`]. However, one couldn't possibly have a key for every single
769/// unicode character that the user might want to type
770/// - so the solution operating systems employ is to allow the user to type these using _a sequence
771/// of keypresses_ instead.
772///
773/// A prominent example of this is accents - many keyboard layouts allow you to first click the
774/// "accent key", and then the character you want to apply the accent to. In this case, some
775/// platforms will generate the following event sequence:
776///
777/// ```ignore
778/// // Press "`" key
779/// Ime::Preedit("`", Some((0, 0)))
780/// // Press "E" key
781/// Ime::Preedit("", None) // Synthetic event generated by winit to clear preedit.
782/// Ime::Commit("é")
783/// ```
784///
785/// Additionally, certain input devices are configured to display a candidate box that allow the
786/// user to select the desired character interactively. (To properly position this box, you must use
787/// [`Window::set_ime_cursor_area`].)
788///
789/// An example of a keyboard layout which uses candidate boxes is pinyin. On a latin keyboard the
790/// following event sequence could be obtained:
791///
792/// ```ignore
793/// // Press "A" key
794/// Ime::Preedit("a", Some((1, 1)))
795/// // Press "B" key
796/// Ime::Preedit("a b", Some((3, 3)))
797/// // Press left arrow key
798/// Ime::Preedit("a b", Some((1, 1)))
799/// // Press space key
800/// Ime::Preedit("啊b", Some((3, 3)))
801/// // Press space key
802/// Ime::Preedit("", None) // Synthetic event generated by winit to clear preedit.
803/// Ime::Commit("啊不")
804/// ```
805#[derive(Debug, Clone, PartialEq, Eq, Hash)]
806#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
807pub enum Ime {
808 /// Notifies when the IME was enabled.
809 ///
810 /// After getting this event you could receive [`Preedit`][Self::Preedit] and
811 /// [`Commit`][Self::Commit] events. You should also start performing IME related requests
812 /// like [`Window::set_ime_cursor_area`].
813 Enabled,
814
815 /// Notifies when a new composing text should be set at the cursor position.
816 ///
817 /// The value represents a pair of the preedit string and the cursor begin position and end
818 /// position. When it's `None`, the cursor should be hidden. When `String` is an empty string
819 /// this indicates that preedit was cleared.
820 ///
821 /// The cursor position is byte-wise indexed.
822 Preedit(String, Option<(usize, usize)>),
823
824 /// Notifies when text should be inserted into the editor widget.
825 ///
826 /// Right before this event winit will send empty [`Self::Preedit`] event.
827 Commit(String),
828
829 /// Notifies when the IME was disabled.
830 ///
831 /// After receiving this event you won't get any more [`Preedit`][Self::Preedit] or
832 /// [`Commit`][Self::Commit] events until the next [`Enabled`][Self::Enabled] event. You should
833 /// also stop issuing IME related requests like [`Window::set_ime_cursor_area`] and clear
834 /// pending preedit text.
835 Disabled,
836}
837
838/// Describes touch-screen input state.
839#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
840#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
841pub enum TouchPhase {
842 Started,
843 Moved,
844 Ended,
845 Cancelled,
846}
847
848/// Represents a touch event
849///
850/// Every time the user touches the screen, a new [`TouchPhase::Started`] event with an unique
851/// identifier for the finger is generated. When the finger is lifted, an [`TouchPhase::Ended`]
852/// event is generated with the same finger id.
853///
854/// After a `Started` event has been emitted, there may be zero or more `Move`
855/// events when the finger is moved or the touch pressure changes.
856///
857/// The finger id may be reused by the system after an `Ended` event. The user
858/// should assume that a new `Started` event received with the same id has nothing
859/// to do with the old finger and is a new finger.
860///
861/// A [`TouchPhase::Cancelled`] event is emitted when the system has canceled tracking this
862/// touch, such as when the window loses focus, or on iOS if the user moves the
863/// device against their face.
864///
865/// ## Platform-specific
866///
867/// - **Web:** Doesn't take into account CSS [`border`], [`padding`], or [`transform`].
868/// - **macOS:** Unsupported.
869///
870/// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
871/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
872/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
873#[derive(Debug, Clone, Copy, PartialEq)]
874pub struct Touch {
875 pub device_id: DeviceId,
876 pub phase: TouchPhase,
877 pub location: PhysicalPosition<f64>,
878 /// Describes how hard the screen was pressed. May be `None` if the platform
879 /// does not support pressure sensitivity.
880 ///
881 /// ## Platform-specific
882 ///
883 /// - Only available on **iOS** 9.0+, **Windows** 8+, **Web**, and **Android**.
884 /// - **Android**: This will never be [None]. If the device doesn't support pressure
885 /// sensitivity, force will either be 0.0 or 1.0. Also see the
886 /// [android documentation](https://developer.android.com/reference/android/view/MotionEvent#AXIS_PRESSURE).
887 pub force: Option<Force>,
888 /// Unique identifier of a finger.
889 pub finger_id: FingerId,
890}
891
892/// Describes the force of a touch event
893#[derive(Debug, Clone, Copy, PartialEq)]
894#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
895pub enum Force {
896 /// On iOS, the force is calibrated so that the same number corresponds to
897 /// roughly the same amount of pressure on the screen regardless of the
898 /// device.
899 Calibrated {
900 /// The force of the touch, where a value of 1.0 represents the force of
901 /// an average touch (predetermined by the system, not user-specific).
902 ///
903 /// The force reported by Apple Pencil is measured along the axis of the
904 /// pencil. If you want a force perpendicular to the device, you need to
905 /// calculate this value using the `altitude_angle` value.
906 force: f64,
907 /// The maximum possible force for a touch.
908 ///
909 /// The value of this field is sufficiently high to provide a wide
910 /// dynamic range for values of the `force` field.
911 max_possible_force: f64,
912 /// The altitude (in radians) of the stylus.
913 ///
914 /// A value of 0 radians indicates that the stylus is parallel to the
915 /// surface. The value of this property is Pi/2 when the stylus is
916 /// perpendicular to the surface.
917 altitude_angle: Option<f64>,
918 },
919 /// If the platform reports the force as normalized, we have no way of
920 /// knowing how much pressure 1.0 corresponds to – we know it's the maximum
921 /// amount of force, but as to how much force, you might either have to
922 /// press really really hard, or not hard at all, depending on the device.
923 Normalized(f64),
924}
925
926impl Force {
927 /// Returns the force normalized to the range between 0.0 and 1.0 inclusive.
928 ///
929 /// Instead of normalizing the force, you should prefer to handle
930 /// [`Force::Calibrated`] so that the amount of force the user has to apply is
931 /// consistent across devices.
932 pub fn normalized(&self) -> f64 {
933 match self {
934 Force::Calibrated { force, max_possible_force, altitude_angle } => {
935 let force = match altitude_angle {
936 Some(altitude_angle) => force / altitude_angle.sin(),
937 None => *force,
938 };
939 force / max_possible_force
940 },
941 Force::Normalized(force) => *force,
942 }
943 }
944}
945
946/// Identifier for a specific analog axis on some device.
947pub type AxisId = u32;
948
949/// Identifier for a specific button on some device.
950pub type ButtonId = u32;
951
952/// Describes the input state of a key.
953#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
954#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
955pub enum ElementState {
956 Pressed,
957 Released,
958}
959
960impl ElementState {
961 /// True if `self == Pressed`.
962 pub fn is_pressed(self) -> bool {
963 self == ElementState::Pressed
964 }
965}
966
967/// Describes a button of a mouse controller.
968///
969/// ## Platform-specific
970///
971/// **macOS:** `Back` and `Forward` might not work with all hardware.
972/// **Orbital:** `Back` and `Forward` are unsupported due to orbital not supporting them.
973#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
974#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
975pub enum MouseButton {
976 Left,
977 Right,
978 Middle,
979 Back,
980 Forward,
981 Other(u16),
982}
983
984/// Describes a difference in the mouse scroll wheel state.
985#[derive(Debug, Clone, Copy, PartialEq)]
986#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
987pub enum MouseScrollDelta {
988 /// Amount in lines or rows to scroll in the horizontal
989 /// and vertical directions.
990 ///
991 /// Positive values indicate that the content that is being scrolled should move
992 /// right and down (revealing more content left and up).
993 LineDelta(f32, f32),
994
995 /// Amount in pixels to scroll in the horizontal and
996 /// vertical direction.
997 ///
998 /// Scroll events are expressed as a `PixelDelta` if
999 /// supported by the device (eg. a touchpad) and
1000 /// platform.
1001 ///
1002 /// Positive values indicate that the content being scrolled should
1003 /// move right/down.
1004 ///
1005 /// For a 'natural scrolling' touch pad (that acts like a touch screen)
1006 /// this means moving your fingers right and down should give positive values,
1007 /// and move the content right and down (to reveal more things left and up).
1008 PixelDelta(PhysicalPosition<f64>),
1009}
1010
1011/// Handle to synchronously change the size of the window from the [`WindowEvent`].
1012#[derive(Debug, Clone)]
1013pub struct SurfaceSizeWriter {
1014 pub(crate) new_surface_size: Weak<Mutex<PhysicalSize<u32>>>,
1015}
1016
1017impl SurfaceSizeWriter {
1018 #[cfg(not(orbital_platform))]
1019 pub(crate) fn new(new_surface_size: Weak<Mutex<PhysicalSize<u32>>>) -> Self {
1020 Self { new_surface_size }
1021 }
1022
1023 /// Try to request surface size which will be set synchronously on the window.
1024 pub fn request_surface_size(
1025 &mut self,
1026 new_surface_size: PhysicalSize<u32>,
1027 ) -> Result<(), RequestError> {
1028 if let Some(inner) = self.new_surface_size.upgrade() {
1029 *inner.lock().unwrap() = new_surface_size;
1030 Ok(())
1031 } else {
1032 Err(RequestError::Ignored)
1033 }
1034 }
1035}
1036
1037impl PartialEq for SurfaceSizeWriter {
1038 fn eq(&self, other: &Self) -> bool {
1039 self.new_surface_size.as_ptr() == other.new_surface_size.as_ptr()
1040 }
1041}
1042
1043impl Eq for SurfaceSizeWriter {}
1044
1045#[cfg(test)]
1046mod tests {
1047 use std::collections::{BTreeSet, HashSet};
1048
1049 use crate::dpi::PhysicalPosition;
1050 use crate::event;
1051
1052 macro_rules! foreach_event {
1053 ($closure:expr) => {{
1054 #[allow(unused_mut)]
1055 let mut x = $closure;
1056 let did = event::DeviceId::dummy();
1057 let fid = event::FingerId::dummy();
1058
1059 #[allow(deprecated)]
1060 {
1061 use crate::event::Event::*;
1062 use crate::event::Ime::Enabled;
1063 use crate::event::WindowEvent::*;
1064 use crate::window::WindowId;
1065
1066 // Mainline events.
1067 let wid = WindowId::dummy();
1068 x(NewEvents(event::StartCause::Init));
1069 x(AboutToWait);
1070 x(LoopExiting);
1071 x(Suspended);
1072 x(Resumed);
1073
1074 // Window events.
1075 let with_window_event = |wev| x(WindowEvent { window_id: wid, event: wev });
1076
1077 with_window_event(CloseRequested);
1078 with_window_event(Destroyed);
1079 with_window_event(Focused(true));
1080 with_window_event(Moved((0, 0).into()));
1081 with_window_event(SurfaceResized((0, 0).into()));
1082 with_window_event(DroppedFile("x.txt".into()));
1083 with_window_event(HoveredFile("x.txt".into()));
1084 with_window_event(HoveredFileCancelled);
1085 with_window_event(Ime(Enabled));
1086 with_window_event(CursorMoved { device_id: did, position: (0, 0).into() });
1087 with_window_event(ModifiersChanged(event::Modifiers::default()));
1088 with_window_event(CursorEntered { device_id: did });
1089 with_window_event(CursorLeft { device_id: did });
1090 with_window_event(MouseWheel {
1091 device_id: did,
1092 delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
1093 phase: event::TouchPhase::Started,
1094 });
1095 with_window_event(MouseInput {
1096 device_id: did,
1097 state: event::ElementState::Pressed,
1098 button: event::MouseButton::Other(0),
1099 });
1100 with_window_event(PinchGesture {
1101 device_id: did,
1102 delta: 0.0,
1103 phase: event::TouchPhase::Started,
1104 });
1105 with_window_event(DoubleTapGesture { device_id: did });
1106 with_window_event(RotationGesture {
1107 device_id: did,
1108 delta: 0.0,
1109 phase: event::TouchPhase::Started,
1110 });
1111 with_window_event(PanGesture {
1112 device_id: did,
1113 delta: PhysicalPosition::<f32>::new(0.0, 0.0),
1114 phase: event::TouchPhase::Started,
1115 });
1116 with_window_event(TouchpadPressure { device_id: did, pressure: 0.0, stage: 0 });
1117 with_window_event(Touch(event::Touch {
1118 device_id: did,
1119 phase: event::TouchPhase::Started,
1120 location: (0.0, 0.0).into(),
1121 finger_id: fid,
1122 force: Some(event::Force::Normalized(0.0)),
1123 }));
1124 with_window_event(ThemeChanged(crate::window::Theme::Light));
1125 with_window_event(Occluded(true));
1126 }
1127
1128 #[allow(deprecated)]
1129 {
1130 use event::DeviceEvent::*;
1131
1132 let with_device_event =
1133 |dev_ev| x(event::Event::DeviceEvent { device_id: did, event: dev_ev });
1134
1135 with_device_event(MouseMotion { delta: (0.0, 0.0).into() });
1136 with_device_event(MouseWheel {
1137 delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
1138 });
1139 with_device_event(Button { button: 0, state: event::ElementState::Pressed });
1140 }
1141 }};
1142 }
1143
1144 #[allow(clippy::redundant_clone)]
1145 #[test]
1146 fn test_event_clone() {
1147 foreach_event!(|event: event::Event| {
1148 let event2 = event.clone();
1149 assert_eq!(event, event2);
1150 })
1151 }
1152
1153 #[test]
1154 fn test_force_normalize() {
1155 let force = event::Force::Normalized(0.0);
1156 assert_eq!(force.normalized(), 0.0);
1157
1158 let force2 =
1159 event::Force::Calibrated { force: 5.0, max_possible_force: 2.5, altitude_angle: None };
1160 assert_eq!(force2.normalized(), 2.0);
1161
1162 let force3 = event::Force::Calibrated {
1163 force: 5.0,
1164 max_possible_force: 2.5,
1165 altitude_angle: Some(std::f64::consts::PI / 2.0),
1166 };
1167 assert_eq!(force3.normalized(), 2.0);
1168 }
1169
1170 #[allow(clippy::clone_on_copy)]
1171 #[test]
1172 fn ensure_attrs_do_not_panic() {
1173 foreach_event!(|event: event::Event| {
1174 let _ = format!("{:?}", event);
1175 });
1176 let _ = event::StartCause::Init.clone();
1177
1178 let did = crate::event::DeviceId::dummy().clone();
1179 let fid = crate::event::FingerId::dummy().clone();
1180 HashSet::new().insert(did);
1181 let mut set = [did, did, did];
1182 set.sort_unstable();
1183 let mut set2 = BTreeSet::new();
1184 set2.insert(did);
1185 set2.insert(did);
1186
1187 HashSet::new().insert(event::TouchPhase::Started.clone());
1188 HashSet::new().insert(event::MouseButton::Left.clone());
1189 HashSet::new().insert(event::Ime::Enabled);
1190
1191 let _ = event::Touch {
1192 device_id: did,
1193 phase: event::TouchPhase::Started,
1194 location: (0.0, 0.0).into(),
1195 finger_id: fid,
1196 force: Some(event::Force::Normalized(0.0)),
1197 }
1198 .clone();
1199 let _ =
1200 event::Force::Calibrated { force: 0.0, max_possible_force: 0.0, altitude_angle: None }
1201 .clone();
1202 }
1203}