OXYGENHUB
◆ main openmsx / src / events / SDLKey.hh
1.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 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
13
namespace openmsx {
14
15
// The combination of:
16
//   SDL_Keysym: to indicate a specific key (key+scancode) and modifiers
17
//   bool:       to indicate up/down
18
struct SDLKey {
19
	SDL_Keysym sym;
20
	bool down; // down=press / up=release
21
22
	[[nodiscard]] static SDLKey create(SDL_Keycode code, SDL_Scancode scancode, bool down, uint16_t mod = 0) {
23
		SDL_Keysym sym;
24
		sym.scancode = scancode; // scancode needs for POSITIONAL mapping mode
25
		sym.sym = code;
26
		sym.mod = mod;
27
		sym.unused = 0; // unicode
28
		return {.sym = sym, .down = down};
29
	}
30
31
	[[nodiscard]] static SDLKey createDown(SDL_Keycode code, SDL_Scancode scancode) {
32
		return create(code, scancode, 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