Avara3D 0.2.0
C++ API reference
DesktopInputContext.h
1//
2// DesktopInputContext.h
3// avara3d
4//
5// Created by Morgan Davis on 12/2/2025.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_INPUT_DESKTOPINPUTCONTEXT_H
10#define AVARA3D_INPUT_DESKTOPINPUTCONTEXT_H
11
12#include <unordered_set>
13
14#include "a3d/Math.h"
15#include "a3d/input/InputContext.h"
16
17namespace a3d {
18
28
29 public:
30 // [Public Types]
31
33 enum class Key : int {
34 Unknown = 0,
35 Space = 32, //+
36 Apostrophe = 39, //+
37 Comma = 44, //+
38 Minus = 45,
39 Period = 46,
40 Slash = 47,
41 Zero = 48,
42 One = 49,
43 Two = 50,
44 Three = 51,
45 Four = 52,
46 Five = 53,
47 Six = 54,
48 Seven = 55,
49 Eight = 56,
50 Nine = 57,
51 Semicolon = 59,
52 Equal = 61,
53 A = 65,
54 B = 66,
55 C = 67,
56 D = 68,
57 E = 69,
58 F = 70,
59 G = 71,
60 H = 72,
61 I = 73,
62 J = 74,
63 K = 75,
64 L = 76,
65 M = 77,
66 N = 78,
67 O = 79,
68 P = 80,
69 Q = 81,
70 R = 82,
71 S = 83,
72 T = 84,
73 U = 85,
74 V = 86,
75 W = 87,
76 X = 88,
77 Y = 89,
78 Z = 90,
79 LeftBracket = 91,
80 Backslash = 92,
81 RightBracket = 93,
82 GraveAccent = 96,
83 World1 = 161, // non-US #1
84 World2 = 162, // non-US #2
85
86 /* Function keys */
87 Escape = 256,
88 Enter = 257,
89 Tab = 258,
90 Backspace = 259,
91 Insert = 260,
92 ForwardDelete = 261,
93 Right = 262,
94 Left = 263,
95 Down = 264,
96 Up = 265,
97 PageUp = 266,
98 PageDown = 267,
99 Home = 268,
100 End = 269,
101 CapsLock = 280,
102 ScrollLock = 281,
103 NumLock = 282,
104 PrintScreen = 283,
105 Pause = 284,
106 F1 = 290,
107 F2 = 291,
108 F3 = 292,
109 F4 = 293,
110 F5 = 294,
111 F6 = 295,
112 F7 = 296,
113 F8 = 297,
114 F9 = 298,
115 F10 = 299,
116 F11 = 300,
117 F12 = 301,
118 F13 = 302,
119 F14 = 303,
120 F15 = 304,
121 F16 = 305,
122 F17 = 306,
123 F18 = 307,
124 F19 = 308,
125 F20 = 309,
126 F21 = 310,
127 F22 = 311,
128 F23 = 312,
129 F24 = 313,
130 F25 = 314,
131 Keypad0 = 320,
132 Keypad1 = 321,
133 Keypad2 = 322,
134 Keypad3 = 323,
135 Keypad4 = 324,
136 Keypad5 = 325,
137 Keypad6 = 326,
138 Keypad7 = 327,
139 Keypad8 = 328,
140 Keypad9 = 329,
141 KeypadDecimal = 330,
142 KeypadDivide = 331,
143 KeypadMultiply = 332,
144 KeypadSubtract = 333,
145 KeypadAdd = 334,
146 KeypadEnter = 335,
147 KeypadEqual = 336,
148 LeftShift = 340,
149 LeftControl = 341,
150 LeftAlt = 342,
151 LeftSuper = 343,
152 RightShift = 344,
153 RightControl = 345,
154 RightAlt = 346,
155 RightSuper = 347,
156 Menu = 348
157 };
158
160 enum class MouseButton : int {
161 One = 0,
162 Two = 1,
163 Three = 2,
164 Four = 3,
165 Five = 4,
166 Six = 5,
167 Seven = 6,
168 Eight = 7
169 };
170
171 // [Public Lifecycle Functions]
172
175 virtual ~DesktopInputContext() = 0;
176
177 DesktopInputContext(const DesktopInputContext& other) = delete;
178 DesktopInputContext& operator=(const DesktopInputContext& other) = delete;
179
180 // [Public Member Functions]
181
183 bool keyDown(Key key);
184
187
189 bool keyPressed(Key key);
190
193
195 bool keyReleased(Key key);
196
199
201 std::unordered_set<Key> keysDown(); // keys currently down
202
204 std::unordered_set<MouseButton> mouseButtonsDown(); // mouse buttons currently down
205
206 // TODO: rework theese.
207
208 // only reports keys down for one query until they are released
210 std::unordered_set<Key> keysPressed();
211
212 // only reports mouse buttons down for one query until they are released
214 std::unordered_set<MouseButton> mouseButtonsPressed();
215
216 // current pointer position in logical viewport coordinates
222 const math::vec2& mousePosition() const;
223
224 // pointer displacement observed during the current input update
232
233 // scroll displacement observed during the current input update
236
237 // [InputContext Internal Member Functions]
238
239 void update(const UpdateInfo& info) override;
240
241 // [Internal Member Functions]
242
243 virtual void rebaseMouseMotion();
244 void releaseAllInputs();
245
246 protected:
247 // [Protected Member Variables]
248
249 std::unordered_set<Key> _keysDown;
250 std::unordered_set<MouseButton> _mouseButtonsDown;
251 std::unordered_set<Key> _keysPressed;
252 std::unordered_set<Key> _keysPressedCleared;
253 std::unordered_set<MouseButton> _mouseButtonsPressed;
254 std::unordered_set<MouseButton> _mouseButtonsPressedCleared;
255 std::unordered_set<Key> _keysReleased;
256 std::unordered_set<MouseButton> _mouseButtonsReleased;
257 math::vec2 _mousePosition;
258 math::vec2 _mousePositionDelta;
259 math::vec2 _pendingMousePositionDelta;
260 math::vec2 _mouseScrollWheelDelta;
261 math::vec2 _pendingMouseScrollWheelDelta;
262 };
263
264}
265
266#endif // AVARA3D_INPUT_DESKTOPINPUTCONTEXT_H
Exposes keyboard, pointer-button, pointer-motion, and scroll state.
std::unordered_set< Key > keysPressed()
Returns and consumes all currently unread key-press transitions.
MouseButton
Identifies one of up to eight pointer buttons.
bool keyPressed(Key key)
Returns and consumes an unread press transition for key.
bool mouseButtonReleased(MouseButton button)
Returns and consumes an unread release transition for button.
const math::vec2 & mouseScrollWheelDelta() const
Returns scroll displacement observed during the current input update.
bool keyReleased(Key key)
Returns and consumes an unread release transition for key.
const math::vec2 & mousePositionDelta() const
Returns pointer displacement observed during the current input update.
bool mouseButtonPressed(MouseButton button)
Returns and consumes an unread press transition for button.
std::unordered_set< Key > keysDown()
Returns a copy of all keys currently held down.
Key
Identifies keyboard keys reported by desktop-style input contexts.
bool mouseButtonDown(MouseButton button)
Returns whether button is currently held down.
DesktopInputContext()
Creates a DesktopInputContext with no active input state.
std::unordered_set< MouseButton > mouseButtonsDown()
Returns a copy of all pointer buttons currently held down.
bool keyDown(Key key)
Returns whether key is currently held down.
std::unordered_set< MouseButton > mouseButtonsPressed()
Returns and consumes all currently unread pointer-button press transitions.
const math::vec2 & mousePosition() const
Returns the current pointer position in logical viewport coordinates.
Base interface for Scene input state updated once per Runner host update.
Definition: InputContext.h:27
Timing information for one input update.
Definition: InputContext.h:33