diff --git a/core/os/input.cpp b/core/os/input.cpp index 0eee3b08692..28a599e6b04 100644 --- a/core/os/input.cpp +++ b/core/os/input.cpp @@ -55,6 +55,7 @@ Input::MouseMode Input::get_mouse_mode() const { void Input::_bind_methods() { ClassDB::bind_method(D_METHOD("is_key_pressed", "scancode"), &Input::is_key_pressed); + ClassDB::bind_method(D_METHOD("is_physical_key_pressed", "scancode"), &Input::is_physical_key_pressed); ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed); ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed); ClassDB::bind_method(D_METHOD("is_action_pressed", "action", "exact"), &Input::is_action_pressed, DEFVAL(false)); diff --git a/core/os/input.h b/core/os/input.h index 8489fcb78df..2d75c9d21ad 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -79,6 +79,7 @@ public: static Input *get_singleton(); virtual bool is_key_pressed(int p_scancode) const = 0; + virtual bool is_physical_key_pressed(int p_scancode) const = 0; virtual bool is_mouse_button_pressed(int p_button) const = 0; virtual bool is_joy_button_pressed(int p_device, int p_button) const = 0; virtual bool is_action_pressed(const StringName &p_action, bool p_exact = false) const = 0; diff --git a/doc/classes/Input.xml b/doc/classes/Input.xml index b8f17d5bae6..64ff22bdde5 100644 --- a/doc/classes/Input.xml +++ b/doc/classes/Input.xml @@ -264,6 +264,13 @@ Returns [code]true[/code] if you are pressing the mouse button specified with [enum ButtonList]. + + + + + Returns [code]true[/code] if you are pressing the key in the physical location on the 101/102-key US QWERTY keyboard. You can pass a [enum KeyList] constant. + + diff --git a/main/input_default.cpp b/main/input_default.cpp index 5e1555fd82c..7ca580f8ede 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -76,6 +76,11 @@ bool InputDefault::is_key_pressed(int p_scancode) const { return keys_pressed.has(p_scancode); } +bool InputDefault::is_physical_key_pressed(int p_scancode) const { + _THREAD_SAFE_METHOD_ + return physical_keys_pressed.has(p_scancode); +} + bool InputDefault::is_mouse_button_pressed(int p_button) const { _THREAD_SAFE_METHOD_ return (mouse_button_mask & (1 << (p_button - 1))) != 0; @@ -316,6 +321,13 @@ void InputDefault::_parse_input_event_impl(const Ref &p_event, bool keys_pressed.erase(k->get_scancode()); } } + if (k.is_valid() && !k->is_echo() && k->get_physical_scancode() != 0) { + if (k->is_pressed()) { + physical_keys_pressed.insert(k->get_physical_scancode()); + } else { + physical_keys_pressed.erase(k->get_physical_scancode()); + } + } Ref mb = p_event; @@ -716,6 +728,7 @@ void InputDefault::release_pressed_events() { flush_buffered_events(); // this is needed to release actions strengths keys_pressed.clear(); + physical_keys_pressed.clear(); joy_buttons_pressed.clear(); _joy_axis.clear(); diff --git a/main/input_default.h b/main/input_default.h index 7efa0450b4f..defdb43a900 100644 --- a/main/input_default.h +++ b/main/input_default.h @@ -39,6 +39,7 @@ class InputDefault : public Input { int mouse_button_mask; + Set physical_keys_pressed; Set keys_pressed; Set joy_buttons_pressed; Map _joy_axis; @@ -221,6 +222,7 @@ protected: public: virtual bool is_key_pressed(int p_scancode) const; + virtual bool is_physical_key_pressed(int p_scancode) const; virtual bool is_mouse_button_pressed(int p_button) const; virtual bool is_joy_button_pressed(int p_device, int p_button) const; virtual bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;