// Luanti // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2010-2013 celeron55, Perttu Ahola #pragma once #include "irrlichttypes.h" #include #include #include #include /* A key press, consisting of a scancode or a keycode. * This fits into 64 bits, so prefer passing this by value. */ class KeyPress { public: KeyPress() = default; KeyPress(const std::string &name); KeyPress(const irr::SEvent::SKeyInput &in); // Get a string representation that is suitable for use in minetest.conf std::string sym() const; // Get a human-readable string representation std::string name() const; // Get the corresponding keycode or KEY_UNKNOWN if one is not available irr::EKEY_CODE getKeycode() const; // Get the corresponding keychar or '\0' if one is not available wchar_t getKeychar() const; // Get the scancode or 0 is one is not available u32 getScancode() const { if (auto pv = std::get_if(&scancode)) return *pv; return 0; } bool operator==(KeyPress o) const { return scancode == o.scancode; } bool operator!=(KeyPress o) const { return !(*this == o); } // Check whether the keypress is valid operator bool() const { return std::holds_alternative(scancode) ? Keycode::isValid(std::get(scancode)) : std::get(scancode) != 0; } static KeyPress getSpecialKey(const std::string &name); private: bool loadFromScancode(const std::string &name); void loadFromKey(irr::EKEY_CODE keycode, wchar_t keychar); std::string formatScancode() const; std::variant scancode = irr::KEY_UNKNOWN; }; // Global defines for convenience // This implementation defers creation of the objects to make sure that the // IrrlichtDevice is initialized. #define EscapeKey KeyPress::getSpecialKey("KEY_ESCAPE") #define LMBKey KeyPress::getSpecialKey("KEY_LBUTTON") #define MMBKey KeyPress::getSpecialKey("KEY_MBUTTON") // Middle Mouse Button #define RMBKey KeyPress::getSpecialKey("KEY_RBUTTON") // Key configuration getter KeyPress getKeySetting(const std::string &settingname); // Clear fast lookup cache void clearKeyCache();