Skip to main content

nadir_platform/
event.rs

1// Copyright 2026 Joseph Anthony Abbott III
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The events a window and the gamepads report.
16
17use nadir_core::{GamepadAxis, GamepadButton, Key, MouseButton, UVec2, Vec2};
18
19use crate::GamepadId;
20
21/// Something that happened to the window or to an input device.
22///
23/// Events arrive in the order the operating system reported them. The
24/// window's come from [`Platform::pump_events`](crate::Platform::pump_events),
25/// and the gamepads' from
26/// [`Platform::poll_gamepads`](crate::Platform::poll_gamepads).
27#[derive(Clone, Copy, Debug, PartialEq)]
28pub enum Event {
29    /// The user asked to close the window. The window stays open: the game
30    /// decides what happens next.
31    CloseRequested,
32    /// The inside of the window has a new size, in physical pixels.
33    Resized(UVec2),
34    /// The scale factor is this from now on. One of these comes before any
35    /// other event.
36    ScaleFactorChanged(f32),
37    /// The window gained (`true`) or lost (`false`) the keyboard focus.
38    ///
39    /// Right after `Focused(false)`, every key and button that was down gets
40    /// a release event, so nothing stays held while the window cannot see it
41    /// go up. They come after it, so that a reader can tell them from the
42    /// releases the user made: a UI then lets go of a held widget without
43    /// taking the release for a click.
44    Focused(bool),
45    /// A key went down or up.
46    Key {
47        /// Which physical key.
48        key: Key,
49        /// Down (`true`) or up (`false`).
50        pressed: bool,
51        /// Whether this is the keyboard repeating a key that is held down.
52        /// Only a press can be a repeat.
53        repeat: bool,
54    },
55    /// A character the user typed, after the key press that typed it.
56    ///
57    /// - One event a character. A character composed of several key presses
58    ///   (a dead key and a letter, a Compose sequence, an input method's
59    ///   choice on Linux and Windows) comes as one event, after the last of
60    ///   them. Those presses are still [`Event::Key`]s.
61    /// - A key held down types its character again with each repeat.
62    /// - Never a control character: Enter, Tab, Backspace, Escape and Delete
63    ///   come only as [`Event::Key`]s.
64    /// - A key pressed with a shortcut's modifier types nothing. On Linux
65    ///   that is Control, Alt or Super, and Alt Gr types. On Windows it is
66    ///   Control without Alt, which with Control is Alt Gr there, or the
67    ///   Windows key; Alt alone is the window menu's. On macOS it is ⌘, or
68    ///   Control without Option, and Option types.
69    /// - macOS has no input method yet: text that only an input method types
70    ///   (in Chinese or Japanese, say) does not come there.
71    Text(char),
72    /// The pointer is here now: physical pixels from the top-left corner of
73    /// the inside of the window, with y growing downward.
74    MouseMoved(Vec2),
75    /// A mouse button went down or up.
76    MouseButton {
77        /// Which button.
78        button: MouseButton,
79        /// Down (`true`) or up (`false`).
80        pressed: bool,
81    },
82    /// The wheel turned, in lines. Positive y is away from the user, and
83    /// positive x is to the right.
84    MouseWheel(Vec2),
85    /// A gamepad was plugged in, or was there when the window opened. It is
86    /// at rest: what is held on it then follows as button and axis events.
87    GamepadConnected(GamepadId),
88    /// A gamepad was unplugged. Before this, it was put back at rest, each
89    /// button held let go and each axis put back at 0 by events of their
90    /// own. Its id is never given again.
91    GamepadDisconnected(GamepadId),
92    /// A gamepad's button went down or up.
93    GamepadButton {
94        /// Which pad.
95        id: GamepadId,
96        /// Which button, by its place on the pad.
97        button: GamepadButton,
98        /// Down (`true`) or up (`false`).
99        pressed: bool,
100    },
101    /// A gamepad's axis has a new value.
102    GamepadAxis {
103        /// Which pad.
104        id: GamepadId,
105        /// Which axis.
106        axis: GamepadAxis,
107        /// -1 to 1 for a stick's axis, with +Y down, and 0 to 1 for a
108        /// trigger. Raw: no dead zone is taken off, so a stick at rest can
109        /// read a little off 0.
110        value: f32,
111    },
112}