JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Key Class Reference

A key on the keyboard: one without a character, e.g. More...

#include <jngl/Key.hpp>

Public Member Functions

 Key (key::KeyType)
 A key without a character, e.g. jngl::key::F1, or key::Ctrl for either of the two.
 
 Key (char character)
 The key that types character, e.g.
 
 Key (std::string character)
 The key that types character, which has to be exactly one UTF-8 character, e.g.
 
std::string toString () const
 Used to display the key, e.g.
 
bool down () const
 Whether the key is down, regardless of Shift, Ctrl and Alt.
 
bool pressed () const
 Whether the key has been pressed since the next to last call to updateInput(), regardless of Shift, Ctrl and Alt.
 
bool operator== (const Key &) const =default
 

Static Public Member Functions

static std::optional< Key > fromString (std::string_view name)
 The key whose toString() is name, or std::nullopt if there's none.
 

Detailed Description

A key on the keyboard: one without a character, e.g.

jngl::key::F1, or the one that types a character, e.g. Q or Ö

Unlike for jngl::keyDown(const std::string&), Shift doesn't matter: ‘jngl::Key('q’)is the same key asjngl::Key('Q')`. That makes it something to store, e.g. the key the user has chosen for an action, see toString() and jngl::keysPressed().

Example:

jngl::Key jump = jngl::key::Space;
if (choosing) {
if (const auto keys = jngl::keysPressed(); !keys.empty()) {
jump = keys.front();
choosing = false;
}
} else if (jump.pressed()) {
player.jump();
}
A key on the keyboard: one without a character, e.g.
Definition Key.hpp:36
bool pressed() const
Whether the key has been pressed since the next to last call to updateInput(), regardless of Shift,...
std::vector< Key > keysPressed()
The keys that have been pressed since the next to last call to updateInput(), in the order they went ...

Definition at line 36 of file Key.hpp.

Constructor & Destructor Documentation

◆ Key() [1/2]

Key ( char character)

The key that types character, e.g.

'q' (the same as 'Q'), or key::Space for ' '

Exceptions
std::runtime_errorif character isn't ASCII

◆ Key() [2/2]

Key ( std::string character)
explicit

The key that types character, which has to be exactly one UTF-8 character, e.g.

"ö" (the same as "Ö"), or key::Space for " "

Exceptions
std::runtime_errorif character isn't exactly one character

Member Function Documentation

◆ toString()

std::string toString ( ) const

Used to display the key, e.g.

"F1", "Space" or "Ö", which fromString() turns back into it

A key without a character gets jngl::keyToString(), one with a letter it in uppercase.


The documentation for this class was generated from the following file: