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 <cstdint>
11#include <functional>
12#include <memory>
13#include <vector>
14#if defined(__has_include) && __has_include(<optional>)
15#include <optional>
16using std::optional;
17#else
18#include <experimental/optional>
20#endif
21
22namespace jngl {
23
24namespace key {
26enum KeyType : uint8_t {
27 Left,
28 Up,
29 Right,
30 Down,
31 PageUp,
32 PageDown,
33 Home,
34 End,
35 BackSpace,
36 Tab,
37 Clear,
38 Return,
39 Pause,
40 Escape,
41 Delete,
42 CtrlL,
43 CtrlR,
44 ControlL = CtrlL,
45 ControlR = CtrlR,
46
49
50 CapsLock,
51
54
57
58 AltL = Alt,
59 AltR = AltGr,
60
63
66
67 Space,
68 ShiftL,
69 ShiftR,
70
73
74 F1,
75 F2,
76 F3,
77 F4,
78 F5,
79 F6,
80 F7,
81 F8,
82 F9,
83 F10,
84 F11,
85 F12,
86 Any
87};
88} // namespace key
89
92
95
97enum KeyboardType : uint8_t { Default, Numpad };
98
101
104
106void setKeyPressed(const std::string& key, bool);
107
110
113
135bool keyDown(char key);
136
158bool keyDown(const std::string& key);
159
162
168bool keyPressed(char key);
169
176bool keyPressed(const std::string& key);
177
181void setRelativeMouseMode(bool relative);
182
185
187void setMouseVisible(bool visible);
188
190[[nodiscard]] Finally hideMouse();
191
197
202
206
212
218
221[[deprecated("Use jngl::getMousePos() instead")]] int getMouseX();
222
225[[deprecated("Use jngl::getMousePos() instead")]] int getMouseY();
226
227namespace mouse {
229enum Button : uint8_t { Left, Middle, Right };
230} // namespace mouse
231
234
236bool mouseDown(mouse::Button button = mouse::Left);
237
239void setMouseDown(mouse::Button button, bool);
240
242bool mousePressed(mouse::Button button = mouse::Left);
243
246
248void setMouse(Vec2 position);
249
251enum class controller : 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
291 Start,
292
294 Back,
295
296 DpadUp,
297 DpadDown,
298 DpadLeft,
299 DpadRight,
300 LeftStick,
301 RightStick,
302 Last
303};
304
305class Controller;
306
312
319[[deprecated("Use jngl::Job::onControllersChanged() instead")]]
320void onControllerChanged(std::function<void()> callback);
321
327[[deprecated("Use jngl::TextInputSession instead")]] std::string getTextInput();
328
329} // 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:26
@ Alt
Left Alt key.
Definition input.hpp:53
@ SuperL
Left Windows key.
Definition input.hpp:62
@ AltGr
Right Alt key (Alt Graph key)
Definition input.hpp:56
@ Ctrl
Shortcut for either CtrlL or CtrlR.
Definition input.hpp:48
@ Shift
Shortcut for either ShiftL or ShiftR.
Definition input.hpp:72
@ SuperR
Right Windows key.
Definition input.hpp:65
Button
Mouse buttons.
Definition input.hpp:229
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:97
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.
controller
Gamepad buttons.
Definition input.hpp:251
@ Back
Back, Menu, or Share button.
@ RightStickXInverse
-RightStickX
@ LeftButton
Left shoulder button.
@ LeftStickYInverse
-LeftStickY
@ LeftStickY
y axis of left stick, -1 to 1
@ RightButton
Right shoulder button.
@ RightStickX
x axis of right stick, -1 to 1
@ LeftStickXInverse
-LeftStickX
@ LeftStickX
x axis of left stick, -1 to 1
@ Start
Start, or Options button.
@ RightStickYInverse
-RightStickY
@ RightStickY
y axis of right stick, -1 to 1