ge211
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 119 of file ge211_event.h.

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 noexcept
 The type of the key.
 
char32_t code () const noexcept
 The Unicode code point of the key, if it has one.
 
bool is_textual () const noexcept
 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 () noexcept
 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 () noexcept
 Constructs the up arrow key.
 
static Key down () noexcept
 Constructs the down arrow key.
 
static Key left () noexcept
 Constructs the left arrow key.
 
static Key right () noexcept
 Constructs the right arrow key.
 
static Key shift () noexcept
 Constructs the shift key.
 
static Key control () noexcept
 Constructs the control key.
 
static Key alt () noexcept
 Constructs the alt (or option) key.
 
static Key command () noexcept
 Constructs the command (or meta) key.
 
static Key other () noexcept
 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 172 of file ge211_event.h.

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

◆ 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 133 of file ge211_event.h.

◆ is_textual()

bool is_textual ( ) const
noexcept

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

◆ other()

static Key other ( )
inlinestaticnoexcept

Constructs an invalid or unknown key.

This returns the same value as the default constructor Key().

Definition at line 167 of file ge211_event.h.


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