Represents a key on the keyboard.
The easiest way to detect a key is to create the Key value you want to detect and then compare for equality. For example, you can create the up arrow Key with Key::up()
, or the uppercase M
key with ‘Key::code('M’)`.
Here's an example of key handling:
Another way to recognize a key is to look at its two properties:
0
otherwise.Here is an example distinguishing several keys by switching on their properties:
Currently this type supports keys that deliver Unicode values using whatever input method is supported by your operating system, as well as the arrow keys and modifier keys shift, control, option/alt, and command/meta. If you need to handle other keys, contact me and I will add them.
Definition at line 127 of file ge211_event.hxx.
Public Types | |
enum | Type { code, up, down, left, right, shift, control, alt, command, other } |
The possible types of keys. More... | |
Public Member Functions | |
Type | type () const |
The type of the key. | |
char32_t | code () const |
The Unicode code point of the key, if it has one. | |
bool | is_textual () const |
Does the key represent printable text? This is true for some but not all Type::code keys. More... | |
std::string | as_text () const |
Returns a representation of the key's code as a std::string. More... | |
Constructor and factories | |
Key () | |
Constructs the empty key, with type Key::Type::other. | |
static Key | code (char32_t c) |
Constructs a key with the given Unicode code point code. More... | |
static Key | up () |
Constructs the up arrow key. | |
static Key | down () |
Constructs the down arrow key. | |
static Key | left () |
Constructs the left arrow key. | |
static Key | right () |
Constructs the right arrow key. | |
static Key | shift () |
Constructs the shift key. | |
static Key | control () |
Constructs the control key. | |
static Key | alt () |
Constructs the alt (or option) key. | |
static Key | command () |
Constructs the command (or meta) key. | |
static Key | other () |
Constructs an invalid or unknown key. More... | |
|
strong |
The possible types of keys.
Enumerator | |
---|---|
code | Indicates a key with an Unicode value, which can be gotten with Key::code() const. |
up | The up arrow key. |
down | The down arrow key. |
left | The left arrow key. |
right | The right arrow key. |
shift | The shift key. |
control | The control key. |
alt | The alt or option key. |
command | The command or meta key. |
other | Any other, unknown or invalid key. |
Definition at line 180 of file ge211_event.hxx.
std::string as_text | ( | ) | const |
Returns a representation of the key's code as a std::string.
This could be useful if you want to capture typing text, rather than game control, because concatenating a string to a string is easier than concatenating the char32_t
code() const to a string, when that could be an arbitrary Unicode code point.
The result of this function is only meaningful when is_textual() const returns true.
Definition at line 141 of file ge211_event.cxx.
|
inlinestatic |
Constructs a key with the given Unicode code point code.
Throws exceptions::Client_logic_error if c
is not a valid Unicode code point. Valid code points are from 0 to 0x10FFFF, except for 0xD800 to 0xDFFF.
Definition at line 141 of file ge211_event.hxx.
bool is_textual | ( | ) | const |
Does the key represent printable text? This is true for some but not all Type::code keys.
It's never true for other types of keys.
Definition at line 136 of file ge211_event.cxx.
|
inlinestatic |
Constructs an invalid or unknown key.
This returns the same value as the default constructor Key().
Definition at line 175 of file ge211_event.hxx.