JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1// Copyright 2012-2025 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "Finally.hpp"
8#include "Vec2.hpp"
9
10#include <functional>
11#include <memory>
12#include <vector>
13#if defined(__has_include) && __has_include(<optional>)
14#include <optional>
15using std::optional;
16#else
17#include <experimental/optional>
19#endif
20
21namespace jngl {
22
23namespace key {
25enum KeyType : uint8_t {
26 Left,
27 Up,
28 Right,
29 Down,
30 PageUp,
31 PageDown,
32 Home,
33 End,
34 BackSpace,
35 Tab,
36 Clear,
37 Return,
38 Pause,
39 Escape,
40 Delete,
41 CtrlL,
42 CtrlR,
43 ControlL = CtrlL,
44 ControlR = CtrlR,
45
48
49 CapsLock,
50
53
56
57 AltL = Alt,
58 AltR = AltGr,
59
62
65
66 Space,
67 ShiftL,
68 ShiftR,
69
72
73 F1,
74 F2,
75 F3,
76 F4,
77 F5,
78 F6,
79 F7,
80 F8,
81 F9,
82 F10,
83 F11,
84 F12,
85 Any
86};
87} // namespace key
88
91
94
96enum KeyboardType : uint8_t { Default, Numpad };
97
100
103
105void setKeyPressed(const std::string& key, bool);
106
109
112
134bool keyDown(char key);
135
157bool keyDown(const std::string& key);
158
161
167bool keyPressed(char key);
168
175bool keyPressed(const std::string& key);
176
180void setRelativeMouseMode(bool relative);
181
184
186void setMouseVisible(bool visible);
187
189[[nodiscard]] Finally hideMouse();
190
196
201
205
211
217
220[[deprecated("Use jngl::getMousePos() instead")]] int getMouseX();
221
224[[deprecated("Use jngl::getMousePos() instead")]] int getMouseY();
225
226namespace mouse {
228enum Button : uint8_t { Left, Middle, Right };
229} // namespace mouse
230
233
235bool mouseDown(mouse::Button button = mouse::Left);
236
238void setMouseDown(mouse::Button button, bool);
239
241bool mousePressed(mouse::Button button = mouse::Left);
242
245
247void setMouse(Vec2 position);
248
249namespace controller {
251enum Button : uint8_t {
254
257
260
263
266
269
272
275
276 A,
277 B,
278 X,
279 Y,
280
283
286
287 LeftTrigger,
288 RightTrigger,
289
292
295
296 DpadUp,
297 DpadDown,
298 DpadLeft,
299 DpadRight,
300 LeftStick,
301 RightStick,
302 Last
303};
304} // namespace controller
305
306class Controller;
307
313
320[[deprecated("Use jngl::Job::onControllersChanged() instead")]]
321void onControllerChanged(std::function<void()> callback);
322
325
326} // namespace jngl
Contains jngl::Finally class.
Contains jngl::Vec2 class.
Helper class which calls a function when being destroyed.
Definition Finally.hpp:22
Two-dimensional vector.
Definition Vec2.hpp:36
KeyType
Keyboard keys.
Definition input.hpp:25
@ Alt
Left Alt key.
Definition input.hpp:52
@ SuperL
Left Windows key.
Definition input.hpp:61
@ AltGr
Right Alt key (Alt Graph key)
Definition input.hpp:55
@ Ctrl
Shortcut for either CtrlL or CtrlR.
Definition input.hpp:47
@ Shift
Shortcut for either ShiftL or ShiftR.
Definition input.hpp:71
@ SuperR
Right Windows key.
Definition input.hpp:64
Button
Mouse buttons.
Definition input.hpp:228
@ RightStickY
y axis of right stick, -1 to 1
Definition input.hpp:271
@ RightStickYInverse
-RightStickY
Definition input.hpp:274
@ Back
Back, Menu, or Share button.
Definition input.hpp:294
@ LeftButton
Left shoulder button.
Definition input.hpp:282
@ RightStickXInverse
-RightStickX
Definition input.hpp:268
@ LeftStickYInverse
-LeftStickY
Definition input.hpp:262
@ LeftStickXInverse
-LeftStickX
Definition input.hpp:256
@ LeftStickX
x axis of left stick, -1 to 1
Definition input.hpp:253
@ RightStickX
x axis of right stick, -1 to 1
Definition input.hpp:265
@ Start
Start, or Options button.
Definition input.hpp:291
@ LeftStickY
y axis of left stick, -1 to 1
Definition input.hpp:259
@ RightButton
Right shoulder button.
Definition input.hpp:285
JNGL's main namespace.
int getMouseX()
Retrieve mouse position in pixels.
void setRelativeMouseMode(bool relative)
Enables relative mouse mode which will hide the cursor and allow for unlimited movement.
KeyboardType
Whether a normal or a numbers-only keyboard should appear.
Definition input.hpp:96
std::string keyToString(key::KeyType)
Returns a string representation of a key useful for displaying it to the user.
double getMouseWheel()
Returns mouse wheel movement between -100 and 100 (0 if the mousewheel doesn't move)
bool keyDown(key::KeyType key)
Whether key is down.
int getMouseY()
Retrieve mouse position in pixels.
KeyboardType getKeyboardType()
Currently active type of onscreen keyboard.
std::vector< jngl::Vec2 > getTouchPositions()
Returns all positions where a finger touches the screen.
void onControllerChanged(std::function< void()> callback)
Specify a function which gets called, whenever a controller gets added or removed.
void setMouse(Vec2 position)
Moves the mouse (does nothing on iOS and Android)
std::string getTextInput()
Returns a string of characters that have been pressed since the last call to updateInput()
bool getRelativeMouseMode()
Whether relative mouse mode is currently active.
bool mousePressed(mouse::Button button=mouse::Left)
Whether button has been pressed since the next to last call to updateInput()
bool isMultitouch()
Returns true when there's more than one finger touching the screen.
bool mouseDown(mouse::Button button=mouse::Left)
Returns whether button is currently held down.
void setMouseDown(mouse::Button button, bool)
Overwrite what mouseDown() should return.
void setKeyboardVisible(bool)
Display onscreen keyboard for touch devices.
void setKeyboardType(KeyboardType)
Type of the onscreen keyboard.
Finally hideMouse()
Hides the mouse cursor; destroying the returned Finally object will show it again.
void setKeyPressed(const std::string &key, bool)
Overwrite what keyPressed() should return.
bool keyPressed(key::KeyType key)
Whether key has been pressed since the next to last call to updateInput()
void setMouseVisible(bool visible)
By default the mouse cursor of the OS is visible and can be hidden by passing false.
std::vector< std::shared_ptr< Controller > > getConnectedControllers()
Returns all controllers (gamepads) that are connected.
void setMousePressed(jngl::mouse::Button, bool)
Overwrite what mouseDown() should return.
Vec2 getMousePos()
Mouse position in screen coordinates.
bool isMouseVisible()
Returns whether the mouse cursor of the OS is currently visible.
optional< Vec2 > getCursorPos()
Returns the position of the mouse pointer if a mouse is connected/available.