diff --git a/src/client/keycode.cpp b/src/client/keycode.cpp index e8dd8bf94..54c33e62e 100644 --- a/src/client/keycode.cpp +++ b/src/client/keycode.cpp @@ -312,6 +312,16 @@ std::string KeyPress::name() const return formatScancode(); } +irr::EKEY_CODE KeyPress::getKeycode() const +{ + return lookup_scancode(scancode).Key; +} + +wchar_t KeyPress::getKeychar() const +{ + return lookup_scancode(scancode).Char; +} + bool KeyPress::loadFromScancode(const std::string_view &name) { if (name.size() < 2 || name[0] != '<') @@ -337,7 +347,7 @@ const KeyPress &KeyPress::getSpecialKey(const std::string &name) // A simple cache for quicker lookup static std::unordered_map g_key_setting_cache; -const KeyPress &getKeySetting(const char *settingname) +const KeyPress &getKeySetting(const std::string &settingname) { auto n = g_key_setting_cache.find(settingname); if (n != g_key_setting_cache.end()) @@ -352,8 +362,3 @@ void clearKeyCache() { g_key_setting_cache.clear(); } - -irr::EKEY_CODE keyname_to_keycode(const char *name) -{ - return lookup_keyname(name).Key; -} diff --git a/src/client/keycode.h b/src/client/keycode.h index b2736c791..2f899061d 100644 --- a/src/client/keycode.h +++ b/src/client/keycode.h @@ -24,6 +24,17 @@ public: std::string sym(const bool force_scancode = false) const; std::string name() const; + irr::EKEY_CODE getKeycode() const; + wchar_t getKeychar() const; + + irr::SEvent toKeyEvent(bool pressedDown = false) const + { + irr::SEvent event; + event.EventType = EET_KEY_INPUT_EVENT; + event.KeyInput = {getKeychar(), getKeycode(), scancode, pressedDown, false, false}; + return event; + } + bool operator==(const KeyPress &o) const { return scancode == o.scancode; } @@ -56,9 +67,7 @@ private: #define RMBKey KeyPress::getSpecialKey("KEY_RBUTTON") // Key configuration getter -const KeyPress &getKeySetting(const char *settingname); +const KeyPress &getKeySetting(const std::string &settingname); // Clear fast lookup cache void clearKeyCache(); - -irr::EKEY_CODE keyname_to_keycode(const char *name); diff --git a/src/gui/touchcontrols.cpp b/src/gui/touchcontrols.cpp index 34c0091c6..8efe8ea31 100644 --- a/src/gui/touchcontrols.cpp +++ b/src/gui/touchcontrols.cpp @@ -13,7 +13,6 @@ #include "porting.h" #include "settings.h" #include "client/guiscalingfilter.h" -#include "client/keycode.h" #include "client/renderingengine.h" #include "client/texturesource.h" #include "util/numeric.h" @@ -29,16 +28,9 @@ TouchControls *g_touchcontrols; -void TouchControls::emitKeyboardEvent(EKEY_CODE keycode, bool pressed) +void TouchControls::emitKeyboardEvent(const KeyPress &key, bool pressed) { - SEvent e{}; - e.EventType = EET_KEY_INPUT_EVENT; - e.KeyInput.Key = keycode; - e.KeyInput.Control = false; - e.KeyInput.Shift = false; - e.KeyInput.Char = 0; - e.KeyInput.PressedDown = pressed; - m_receiver->OnEvent(e); + m_receiver->OnEvent(key.toKeyEvent(pressed)); } void TouchControls::loadButtonTexture(IGUIImage *gui_button, const std::string &path) @@ -52,10 +44,10 @@ void TouchControls::loadButtonTexture(IGUIImage *gui_button, const std::string & void TouchControls::buttonEmitAction(button_info &btn, bool action) { - if (btn.keycode == KEY_UNKNOWN) + if (!btn.keypress) return; - emitKeyboardEvent(btn.keycode, action); + emitKeyboardEvent(btn.keypress, action); if (action) { if (btn.toggleable == button_info::FIRST_TEXTURE) { @@ -131,12 +123,11 @@ bool TouchControls::buttonsStep(std::vector &buttons, float dtime) return has_pointers; } -static EKEY_CODE id_to_keycode(touch_gui_button_id id) +static const KeyPress &id_to_keypress(touch_gui_button_id id) { - EKEY_CODE code; // ESC isn't part of the keymap. if (id == exit_id) - return KEY_ESCAPE; + return EscapeKey; std::string key = ""; switch (id) { @@ -189,9 +180,7 @@ static EKEY_CODE id_to_keycode(touch_gui_button_id id) break; } assert(!key.empty()); - std::string resolved = g_settings->get("keymap_" + key); - code = keyname_to_keycode(resolved.c_str()); - return code; + return getKeySetting("keymap_" + key); } @@ -317,7 +306,7 @@ bool TouchControls::mayAddButton(touch_gui_button_id id) return false; if (id == aux1_id && m_joystick_triggers_aux1) return false; - if (id != overflow_id && id_to_keycode(id) == KEY_UNKNOWN) + if (id != overflow_id && !id_to_keypress(id)) return false; return true; } @@ -330,7 +319,7 @@ void TouchControls::addButton(std::vector &buttons, touch_gui_butto loadButtonTexture(btn_gui_button, image); button_info &btn = buttons.emplace_back(); - btn.keycode = id_to_keycode(id); + btn.keypress = id_to_keypress(id); btn.gui_button = grab_gui_element(btn_gui_button); } @@ -595,7 +584,7 @@ void TouchControls::translateEvent(const SEvent &event) void TouchControls::applyJoystickStatus() { if (m_joystick_triggers_aux1) { - auto key = id_to_keycode(aux1_id); + auto key = id_to_keypress(aux1_id); emitKeyboardEvent(key, false); if (m_joystick_status_aux1) emitKeyboardEvent(key, true); diff --git a/src/gui/touchcontrols.h b/src/gui/touchcontrols.h index bcd0f3178..6af02c719 100644 --- a/src/gui/touchcontrols.h +++ b/src/gui/touchcontrols.h @@ -16,6 +16,7 @@ #include "itemdef.h" #include "touchscreenlayout.h" #include "util/basic_macros.h" +#include "client/keycode.h" namespace irr { @@ -57,7 +58,7 @@ enum class TapState struct button_info { float repeat_counter; - EKEY_CODE keycode; + KeyPress keypress; std::vector pointer_ids; std::shared_ptr gui_button = nullptr; @@ -187,7 +188,7 @@ private: // for its buttons. We only want static image display, not interactivity, // from Irrlicht. - void emitKeyboardEvent(EKEY_CODE keycode, bool pressed); + void emitKeyboardEvent(const KeyPress &keycode, bool pressed); void loadButtonTexture(IGUIImage *gui_button, const std::string &path); void buttonEmitAction(button_info &btn, bool action);