Class KeyboardHandlerUnixImpl

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

Class Documentation

class KeyboardHandlerUnixImpl : public KeyboardHandlerBase

Unix (Posix) specific implementation of keyboard handler class.

Note

Design and implementation limitations: Can’t correctly detect CTRL + 0..9 number keys. Can’t correctly detect CTRL, ALT, SHIFT modifiers with F1..F12 and other control keys. Instead of CTRL + SHIFT + key will be detected only CTRL + key. Some keys might be incorrectly detected with multiple key modifiers pressed at the same time.

Public Types

using isattyFunction = std::function<int(int)>
using tcgetattrFunction = std::function<int(int, struct termios*)>
using tcsetattrFunction = std::function<int(int, int, const struct termios*)>
using readFunction = std::function<ssize_t(int, void*, size_t)>
using signal_handler_type = void (*)(int)

Public Functions

KEYBOARD_HANDLER_PUBLIC KeyboardHandlerUnixImpl()

Default constructor.

explicit KEYBOARD_HANDLER_PUBLIC KeyboardHandlerUnixImpl(bool install_signal_handler)

Constructor with option to not install signal handler for SIGINT.

Note

In case if install_signal_handler is false caller code should call static KeyboardHandlerUnixImpl::restore_buffer_mode_for_stdin() in case of process termination caused by signal arrival.

Parameters

install_signal_handler – if true signal handler for SIGINT will be installed, otherwise not.

virtual KEYBOARD_HANDLER_PUBLIC ~KeyboardHandlerUnixImpl()

destructor

KEYBOARD_HANDLER_PUBLIC std::string get_terminal_sequence (KeyboardHandlerUnixImpl::KeyCode key_code)

Translates specified key press combination to the corresponding registered sequence of characters returning by terminal in response to the pressing keyboard keys.

Parameters

key_code – Value from enum which corresponds to some predefined key press combination.

Returns

Returns string with sequence of characters expecting to be returned by terminal.

Public Static Functions

static KEYBOARD_HANDLER_PUBLIC bool restore_buffer_mode_for_stdin ()

Restore buffer mode for stdin.

static KEYBOARD_HANDLER_PUBLIC signal_handler_type get_old_sigint_handler ()

Protected Functions

KEYBOARD_HANDLER_PUBLIC KeyboardHandlerUnixImpl(const readFunction &read_fn, const isattyFunction &isatty_fn, const tcgetattrFunction &tcgetattr_fn, const tcsetattrFunction &tcsetattr_fn, bool install_signal_handler = true)

Constructor with references to the system functions. Required for unit tests.

Parameters
  • read_fn – Reference to the system read(int, void *, size_t) function

  • isatty_fn – Reference to the system isatty(int) function

  • tcgetattr_fn – Reference to the system tcgetattr(int, struct termios *) function

  • tcsetattr_fn – Reference to the system tcsetattr(int, int, const struct termios *) function

std::tuple<KeyCode, KeyModifiers> parse_input(const char *buff, ssize_t read_bytes)

Input parser.

Parameters
  • buff – null terminated buffer read out from std::in after key press

  • read_bytes – length of the buffer in bytes without null terminator

Returns

tuple key code and code modifiers mask

Protected Static Attributes

static const KeyMap DEFAULT_STATIC_KEY_MAP[]

Default statically defined lookup table for corresponding KeyCode enum values and expecting sequence of characters returning by terminal.

static const size_t STATIC_KEY_MAP_LENGTH

Length of DEFAULT_STATIC_KEY_MAP measured in number of elements.

struct KeyMap

Data type for mapping KeyCode enum value to the expecting sequence of characters returning by terminal.

Public Members

KeyCode inner_code
const char *terminal_sequence