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
192void setMouseConfined(bool confined);
193
196
198void setMouseVisible(bool visible);
199
201[[nodiscard]] Finally hideMouse();
202
208
213
217
223
229
232[[deprecated("Use jngl::getMousePos() instead")]] int getMouseX();
233
236[[deprecated("Use jngl::getMousePos() instead")]] int getMouseY();
237
238namespace mouse {
240enum Button : uint8_t { Left, Middle, Right };
241} // namespace mouse
242
245
247bool mouseDown(mouse::Button button = mouse::Left);
248
250void setMouseDown(mouse::Button button, bool);
251
253bool mousePressed(mouse::Button button = mouse::Left);
254
257
259void setMouse(Vec2 position);
260
262enum class controller : uint8_t {
265
268
271
274
277
280
283
286
287 A,
288 B,
289 X,
290 Y,
291
294
297
298 LeftTrigger,
299 RightTrigger,
300
302 Start,
303
305 Back,
306
307 DpadUp,
308 DpadDown,
309 DpadLeft,
310 DpadRight,
311 LeftStick,
312 RightStick,
313 Last
314};
315
316class Controller;
317
323
330[[deprecated("Use jngl::Job::onControllersChanged() instead")]]
331void onControllerChanged(std::function<void()> callback);
332
338[[deprecated("Use jngl::TextInputSession instead")]] std::string getTextInput();
339
340} // 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:240
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.
bool isMouseConfined()
Whether setMouseConfined(true) has been called, regardless of the window having focus.
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 setMouseConfined(bool confined)
Keeps the mouse cursor from leaving the window, e.g.
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:262
@ 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