openMSX
SDLKey.hh
Go to the documentation of this file.
1#ifndef SDLKEY_HH
2#define SDLKEY_HH
3
4#include "zstring_view.hh"
5
6#include <SDL.h>
7
8#include <cstdint>
9#include <optional>
10#include <string>
11#include <string_view>
12
13namespace openmsx {
14
15// The combination of:
16// SDL_Keysym: to indicate a specific key (key+scancode) and modifiers
17// bool: to indicate up/down
18struct SDLKey {
19 SDL_Keysym sym;
20 bool down; // down=press / up=release
21
22 [[nodiscard]] static SDLKey create(SDL_Keycode code, bool down, uint16_t mod = 0) {
23 SDL_Keysym sym;
24 sym.scancode = SDL_SCANCODE_UNKNOWN;
25 sym.sym = code;
26 sym.mod = mod;
27 sym.unused = 0; // unicode
28 return {sym, down};
29 }
30
31 [[nodiscard]] static SDLKey createDown(SDL_Keycode code) {
32 return create(code, true);
33 }
34
35 [[nodiscard]] static std::optional<SDLKey> fromString(std::string_view name);
36 [[nodiscard]] static SDL_Keycode keycodeFromString(zstring_view name);
37
38 // only uses the 'sym.sym', 'sym.mod' and 'down' fields (ignores sym.scancode and 'sym.unicode')
39 [[nodiscard]] std::string toString() const;
40 [[nodiscard]] static std::string toString(SDL_Keycode code);
41};
42
43} // namespace openmsx
44
45#endif
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
Definition Autofire.cc:11
static SDL_Keycode keycodeFromString(zstring_view name)
Definition SDLKey.cc:109
SDL_Keysym sym
Definition SDLKey.hh:19
static std::optional< SDLKey > fromString(std::string_view name)
Definition SDLKey.cc:119
static SDLKey createDown(SDL_Keycode code)
Definition SDLKey.hh:31
std::string toString() const
Definition SDLKey.cc:230
static SDLKey create(SDL_Keycode code, bool down, uint16_t mod=0)
Definition SDLKey.hh:22