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 ControlL,
42 ControlR,
43
46
47 CapsLock,
48 AltL,
49 AltR,
50
53
56
57 Space,
58 ShiftL,
59 ShiftR,
60 F1,
61 F2,
62 F3,
63 F4,
64 F5,
65 F6,
66 F7,
67 F8,
68 F9,
69 F10,
70 F11,
71 F12,
72 Any
73};
74} // namespace key
75
78
80enum KeyboardType : uint8_t { Default, Numpad };
81
84
87
89void setKeyPressed(const std::string& key, bool);
90
93
96
98bool keyDown(char key);
99
117bool keyDown(const std::string& key);
118
121
123bool keyPressed(char key);
124
127bool keyPressed(const std::string& key);
128
129void setRelativeMouseMode(bool relative);
130
131bool getRelativeMouseMode();
132
134void setMouseVisible(bool visible);
135
137[[nodiscard]] Finally hideMouse();
138
144
149
153
159
165
168[[deprecated("Use jngl::getMousePos() instead")]] int getMouseX();
169
172[[deprecated("Use jngl::getMousePos() instead")]] int getMouseY();
173
174namespace mouse {
176enum Button : uint8_t { Left, Middle, Right };
177} // namespace mouse
178
181
183bool mouseDown(mouse::Button button = mouse::Left);
184
186void setMouseDown(mouse::Button button, bool);
187
189bool mousePressed(mouse::Button button = mouse::Left);
190
193
195void setMouse(Vec2 position);
196
197namespace controller {
199enum Button : uint8_t {
202
205
208
211
214
217
220
223
224 A,
225 B,
226 X,
227 Y,
228
231
234
235 LeftTrigger,
236 RightTrigger,
237
240
243
244 DpadUp,
245 DpadDown,
246 DpadLeft,
247 DpadRight,
248 LeftStick,
249 RightStick,
250 Last
251};
252} // namespace controller
253
254class Controller;
255
261
268[[deprecated("Use jngl::Job::onControllersChanged() instead")]]
269void onControllerChanged(std::function<void()> callback);
270
273
274} // 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
@ SuperL
Left Windows key.
Definition input.hpp:52
@ Ctrl
Shortcut for either ControlL or ControlR.
Definition input.hpp:45
@ SuperR
Right Windows key.
Definition input.hpp:55
Button
Mouse buttons.
Definition input.hpp:176
@ RightStickY
y axis of right stick, -1 to 1
Definition input.hpp:219
@ RightStickYInverse
-RightStickY
Definition input.hpp:222
@ Back
Back, Menu, or Share button.
Definition input.hpp:242
@ LeftButton
Left shoulder button.
Definition input.hpp:230
@ RightStickXInverse
-RightStickX
Definition input.hpp:216
@ LeftStickYInverse
-LeftStickY
Definition input.hpp:210
@ LeftStickXInverse
-LeftStickX
Definition input.hpp:204
@ LeftStickX
x axis of left stick, -1 to 1
Definition input.hpp:201
@ RightStickX
x axis of right stick, -1 to 1
Definition input.hpp:213
@ Start
Start, or Options button.
Definition input.hpp:239
@ LeftStickY
y axis of left stick, -1 to 1
Definition input.hpp:207
@ RightButton
Right shoulder button.
Definition input.hpp:233
JNGL's main namespace.
int getMouseX()
Retrieve mouse position in pixels.
KeyboardType
Whether a normal or a numbers-only keyboard should appear.
Definition input.hpp:80
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 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.