Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
keycode.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include <Keycodes.h>
9#include <IEventReceiver.h>
10#include <string>
11#include <variant>
12
13/* A key press, consisting of a scancode or a keycode.
14 * This fits into 64 bits, so prefer passing this by value.
15*/
17{
18public:
19 KeyPress() = default;
20
21 KeyPress(const std::string &name);
22
23 KeyPress(const SEvent::SKeyInput &in);
24
25 // Get a string representation that is suitable for use in minetest.conf
26 std::string sym() const;
27
28 // Get a human-readable string representation
29 std::string name() const;
30
31 // Get the corresponding keycode or KEY_UNKNOWN if one is not available
32 EKEY_CODE getKeycode() const;
33
34 // Get the corresponding keychar or '\0' if one is not available
35 wchar_t getKeychar() const;
36
37 // Get the scancode or 0 is one is not available
38 u32 getScancode() const
39 {
40 if (auto pv = std::get_if<u32>(&scancode))
41 return *pv;
42 return 0;
43 }
44
45 bool operator==(KeyPress o) const {
46 return scancode == o.scancode;
47 }
48 bool operator!=(KeyPress o) const {
49 return !(*this == o);
50 }
51
52 // Used for e.g. std::set
53 bool operator<(KeyPress o) const {
54 return scancode < o.scancode;
55 }
56
57 // Check whether the keypress is valid
58 operator bool() const
59 {
60 return std::holds_alternative<EKEY_CODE>(scancode) ?
61 Keycode::isValid(std::get<EKEY_CODE>(scancode)) :
62 std::get<u32>(scancode) != 0;
63 }
64
65 static KeyPress getSpecialKey(const std::string &name);
66
67private:
68 using value_type = std::variant<u32, EKEY_CODE>;
69 bool loadFromScancode(const std::string &name);
70 void loadFromKey(EKEY_CODE keycode, wchar_t keychar);
71 std::string formatScancode() const;
72
73 value_type scancode = KEY_UNKNOWN;
74
75 friend std::hash<KeyPress>;
76};
77
78template <>
79struct std::hash<KeyPress>
80{
81 size_t operator()(KeyPress kp) const noexcept {
82 return std::hash<KeyPress::value_type>{}(kp.scancode);
83 }
84};
85
86// Global defines for convenience
87// This implementation defers creation of the objects to make sure that the
88// IrrlichtDevice is initialized.
89#define EscapeKey KeyPress::getSpecialKey("KEY_ESCAPE")
90#define LMBKey KeyPress::getSpecialKey("KEY_LBUTTON")
91#define MMBKey KeyPress::getSpecialKey("KEY_MBUTTON") // Middle Mouse Button
92#define RMBKey KeyPress::getSpecialKey("KEY_RBUTTON")
93
94// Key configuration getter
95KeyPress getKeySetting(const std::string &settingname);
96
97// Clear fast lookup cache
98void clearKeyCache();
Definition keycode.h:17
void loadFromKey(EKEY_CODE keycode, wchar_t keychar)
Definition keycode.cpp:286
u32 getScancode() const
Definition keycode.h:38
EKEY_CODE getKeycode() const
Definition keycode.cpp:332
bool loadFromScancode(const std::string &name)
Definition keycode.cpp:342
static KeyPress getSpecialKey(const std::string &name)
Definition keycode.cpp:355
std::string sym() const
Definition keycode.cpp:314
std::string name() const
Definition keycode.cpp:322
bool operator!=(KeyPress o) const
Definition keycode.h:48
wchar_t getKeychar() const
Definition keycode.cpp:337
std::variant< u32, EKEY_CODE > value_type
Definition keycode.h:68
bool operator==(KeyPress o) const
Definition keycode.h:45
bool operator<(KeyPress o) const
Definition keycode.h:53
std::string formatScancode() const
Definition keycode.cpp:307
KeyPress()=default
value_type scancode
Definition keycode.h:73
KeyPress getKeySetting(const std::string &settingname)
Definition keycode.cpp:370
void clearKeyCache()
Definition keycode.cpp:385
size_t operator()(KeyPress kp) const noexcept
Definition keycode.h:81