Luanti 5.16.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#include <vector>
13
14/* A key press, consisting of a scancode or a keycode.
15 * This fits into 64 bits, so prefer passing this by value.
16*/
18{
19public:
20 KeyPress() = default;
21
22 KeyPress(const std::string &name);
23
24 KeyPress(const SEvent::SKeyInput &in);
25
26 // Get a string representation that is suitable for use in minetest.conf
27 std::string sym() const;
28
29 // Get a human-readable string representation
30 std::string name() const;
31
32 // Get the corresponding keycode or KEY_UNKNOWN if one is not available
33 EKEY_CODE getKeycode() const;
34
35 // Get the corresponding keychar or '\0' if one is not available
36 wchar_t getKeychar() const;
37
38 // Get the scancode or 0 is one is not available
39 u32 getScancode() const
40 {
41 if (auto pv = std::get_if<u32>(&scancode))
42 return *pv;
43 return 0;
44 }
45
46 bool operator==(KeyPress o) const {
47 return scancode == o.scancode;
48 }
49 bool operator!=(KeyPress o) const {
50 return !(*this == o);
51 }
52
53 // Used for e.g. std::set
54 bool operator<(KeyPress o) const {
55 return scancode < o.scancode;
56 }
57
58 // Check whether the keypress is valid
59 operator bool() const
60 {
61 return std::holds_alternative<EKEY_CODE>(scancode) ?
62 Keycode::isValid(std::get<EKEY_CODE>(scancode)) :
63 std::get<u32>(scancode) != 0;
64 }
65
66 static KeyPress getSpecialKey(const std::string &name);
67
68private:
69 using value_type = std::variant<u32, EKEY_CODE>;
70 bool loadFromScancode(const std::string &name);
71 void loadFromKey(EKEY_CODE keycode, wchar_t keychar);
72 std::string formatScancode() const;
73
74 value_type scancode = KEY_UNKNOWN;
75
76 friend std::hash<KeyPress>;
77};
78
79template <>
80struct std::hash<KeyPress>
81{
82 size_t operator()(KeyPress kp) const noexcept {
83 return std::hash<KeyPress::value_type>{}(kp.scancode);
84 }
85};
86
87// Global defines for convenience
88// This implementation defers creation of the objects to make sure that the
89// IrrlichtDevice is initialized.
90#define EscapeKey KeyPress::getSpecialKey("KEY_ESCAPE")
91#define LMBKey KeyPress::getSpecialKey("KEY_LBUTTON")
92#define MMBKey KeyPress::getSpecialKey("KEY_MBUTTON") // Middle Mouse Button
93#define RMBKey KeyPress::getSpecialKey("KEY_RBUTTON")
94
95// Key configuration getter
96// Note that the reference may be invalidated by a next call to getKeySetting
97// or a related function, so the value should either be used immediately or
98// copied elsewhere before calling this again.
99const std::vector<KeyPress> &getKeySetting(const std::string &settingname);
100
101// Check whether the key setting includes a key.
102bool keySettingHasMatch(const std::string &settingname, KeyPress kp);
103
104// Clear fast lookup cache
105void clearKeyCache();
Definition keycode.h:18
void loadFromKey(EKEY_CODE keycode, wchar_t keychar)
Definition keycode.cpp:289
u32 getScancode() const
Definition keycode.h:39
EKEY_CODE getKeycode() const
Definition keycode.cpp:335
bool loadFromScancode(const std::string &name)
Definition keycode.cpp:345
static KeyPress getSpecialKey(const std::string &name)
Definition keycode.cpp:358
std::string sym() const
Definition keycode.cpp:317
std::string name() const
Definition keycode.cpp:325
bool operator!=(KeyPress o) const
Definition keycode.h:49
wchar_t getKeychar() const
Definition keycode.cpp:340
std::variant< u32, EKEY_CODE > value_type
Definition keycode.h:69
bool operator==(KeyPress o) const
Definition keycode.h:46
bool operator<(KeyPress o) const
Definition keycode.h:54
std::string formatScancode() const
Definition keycode.cpp:310
KeyPress()=default
value_type scancode
Definition keycode.h:74
const std::vector< KeyPress > & getKeySetting(const std::string &settingname)
Definition keycode.cpp:373
bool keySettingHasMatch(const std::string &settingname, KeyPress kp)
Definition keycode.cpp:391
void clearKeyCache()
Definition keycode.cpp:397
size_t operator()(KeyPress kp) const noexcept
Definition keycode.h:82