ge211  2021.5.1
A student game engine
Key Class Reference

Detailed Description

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:

void on_key(Key key) override
{
if (key == Key::code('q'))
quit();
else if (key == Key::up())
move_up();
else if (key == Key::down())
move_down();
}

Another way to recognize a key is to look at its two properties:

Here is an example distinguishing several keys by switching on their properties:

void on_key(Key key) override
{
switch (key.type()) {
move_up();
break;
move_down();
break;
char c = key.code();
switch (c) {
case '\b':
backspace();
break;
case '\r':
enter();
break;
default:
add_to_buffer(c);
}
break;
}
}

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...
 

Member Enumeration Documentation

◆ Type

enum Type
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.

Member Function Documentation

◆ as_text()

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.

◆ code()

static Key code ( char32_t  c)
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.

◆ is_textual()

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.

◆ other()

static Key other ( )
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.


The documentation for this class was generated from the following files:
ge211::events::Key::Type::up
@ up
The up arrow key.
ge211::events::Key::down
static Key down()
Constructs the down arrow key.
Definition: ge211_event.hxx:153
ge211::events::Key::Type::down
@ down
The down arrow key.
ge211::events::Key::up
static Key up()
Constructs the up arrow key.
Definition: ge211_event.hxx:150
ge211::events::Key::Type::code
@ code
Indicates a key with an Unicode value, which can be gotten with Key::code() const.
ge211::events::Key::code
char32_t code() const
The Unicode code point of the key, if it has one.
Definition: ge211_event.hxx:209
ge211::events::Key::Key
Key()
Constructs the empty key, with type Key::Type::other.
Definition: ge211_event.hxx:134