Skip to main content

khora_core/platform/
input.rs

1// Copyright 2025 eraflo
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//! Abstract input event types used by the engine core.
16//!
17//! These types are backend-agnostic. Concrete windowing backends (winit,
18//! SDL, etc.) translate their native events into these abstract events via
19//! adapter functions in `khora-infra`.
20
21/// An engine-internal representation of a user input event.
22///
23/// This enum is backend-agnostic and represents the high-level input actions
24/// that the engine's systems can respond to.
25#[derive(Debug, Clone, PartialEq)]
26pub enum InputEvent {
27    /// A keyboard key was pressed (initial press, no auto-repeat).
28    KeyPressed {
29        /// The physical key code, type-safe.
30        key_code: KeyCode,
31    },
32    /// A keyboard key was released.
33    KeyReleased {
34        /// The physical key code, type-safe.
35        key_code: KeyCode,
36    },
37    /// A mouse button was pressed.
38    MouseButtonPressed {
39        /// The mouse button that was pressed.
40        button: MouseButton,
41    },
42    /// A mouse button was released.
43    MouseButtonReleased {
44        /// The mouse button that was released.
45        button: MouseButton,
46    },
47    /// The mouse cursor moved.
48    MouseMoved {
49        /// The new x-coordinate of the cursor (window pixels).
50        x: f32,
51        /// The new y-coordinate of the cursor (window pixels).
52        y: f32,
53    },
54    /// The mouse wheel was scrolled.
55    MouseWheelScrolled {
56        /// Horizontal scroll delta.
57        delta_x: f32,
58        /// Vertical scroll delta.
59        delta_y: f32,
60    },
61}
62
63/// An engine-internal representation of a mouse button.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65pub enum MouseButton {
66    /// The left mouse button.
67    Left,
68    /// The right mouse button.
69    Right,
70    /// The middle mouse button.
71    Middle,
72    /// The back mouse button (typically on the side).
73    Back,
74    /// The forward mouse button (typically on the side).
75    Forward,
76    /// Another mouse button, identified by a numeric code.
77    Other(u16),
78}
79
80/// Type-safe physical key codes — a 1-to-1 mirror of `winit::keyboard::KeyCode`
81/// but living in `khora-core` so app code never has to depend on winit.
82///
83/// Variant names follow the W3C UI Events `code` attribute convention
84/// (`KeyA`, `Digit1`, `ShiftLeft`, …) — same as winit. The
85/// [`Unidentified`](Self::Unidentified) catch-all is used by the platform
86/// adapter for keys that don't have a recognised code.
87#[allow(missing_docs)]
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
89pub enum KeyCode {
90    Backquote,
91    Backslash,
92    BracketLeft,
93    BracketRight,
94    Comma,
95    Digit0,
96    Digit1,
97    Digit2,
98    Digit3,
99    Digit4,
100    Digit5,
101    Digit6,
102    Digit7,
103    Digit8,
104    Digit9,
105    Equal,
106    IntlBackslash,
107    IntlRo,
108    IntlYen,
109    KeyA,
110    KeyB,
111    KeyC,
112    KeyD,
113    KeyE,
114    KeyF,
115    KeyG,
116    KeyH,
117    KeyI,
118    KeyJ,
119    KeyK,
120    KeyL,
121    KeyM,
122    KeyN,
123    KeyO,
124    KeyP,
125    KeyQ,
126    KeyR,
127    KeyS,
128    KeyT,
129    KeyU,
130    KeyV,
131    KeyW,
132    KeyX,
133    KeyY,
134    KeyZ,
135    Minus,
136    Period,
137    Quote,
138    Semicolon,
139    Slash,
140    AltLeft,
141    AltRight,
142    Backspace,
143    CapsLock,
144    ContextMenu,
145    ControlLeft,
146    ControlRight,
147    Enter,
148    SuperLeft,
149    SuperRight,
150    ShiftLeft,
151    ShiftRight,
152    Space,
153    Tab,
154    Convert,
155    KanaMode,
156    Lang1,
157    Lang2,
158    Lang3,
159    Lang4,
160    Lang5,
161    NonConvert,
162    Delete,
163    End,
164    Help,
165    Home,
166    Insert,
167    PageDown,
168    PageUp,
169    ArrowDown,
170    ArrowLeft,
171    ArrowRight,
172    ArrowUp,
173    NumLock,
174    Numpad0,
175    Numpad1,
176    Numpad2,
177    Numpad3,
178    Numpad4,
179    Numpad5,
180    Numpad6,
181    Numpad7,
182    Numpad8,
183    Numpad9,
184    NumpadAdd,
185    NumpadBackspace,
186    NumpadClear,
187    NumpadClearEntry,
188    NumpadComma,
189    NumpadDecimal,
190    NumpadDivide,
191    NumpadEnter,
192    NumpadEqual,
193    NumpadHash,
194    NumpadMemoryAdd,
195    NumpadMemoryClear,
196    NumpadMemoryRecall,
197    NumpadMemoryStore,
198    NumpadMemorySubtract,
199    NumpadMultiply,
200    NumpadParenLeft,
201    NumpadParenRight,
202    NumpadStar,
203    NumpadSubtract,
204    Escape,
205    Fn,
206    FnLock,
207    PrintScreen,
208    ScrollLock,
209    Pause,
210    BrowserBack,
211    BrowserFavorites,
212    BrowserForward,
213    BrowserHome,
214    BrowserRefresh,
215    BrowserSearch,
216    BrowserStop,
217    Eject,
218    LaunchApp1,
219    LaunchApp2,
220    LaunchMail,
221    MediaPlayPause,
222    MediaSelect,
223    MediaStop,
224    MediaTrackNext,
225    MediaTrackPrevious,
226    Power,
227    Sleep,
228    AudioVolumeDown,
229    AudioVolumeMute,
230    AudioVolumeUp,
231    WakeUp,
232    Meta,
233    Hyper,
234    Turbo,
235    Abort,
236    Resume,
237    Suspend,
238    Again,
239    Copy,
240    Cut,
241    Find,
242    Open,
243    Paste,
244    Props,
245    Select,
246    Undo,
247    Hiragana,
248    Katakana,
249    F1,
250    F2,
251    F3,
252    F4,
253    F5,
254    F6,
255    F7,
256    F8,
257    F9,
258    F10,
259    F11,
260    F12,
261    F13,
262    F14,
263    F15,
264    F16,
265    F17,
266    F18,
267    F19,
268    F20,
269    F21,
270    F22,
271    F23,
272    F24,
273    F25,
274    F26,
275    F27,
276    F28,
277    F29,
278    F30,
279    F31,
280    F32,
281    F33,
282    F34,
283    F35,
284    /// Catch-all for keys whose physical code the adapter could not map to
285    /// any of the variants above. The platform adapter logs a one-shot
286    /// warning the first time it produces this value so the gap is visible.
287    Unidentified,
288}