diff options
-rw-r--r-- | core/os/input_event.cpp | 39 | ||||
-rw-r--r-- | core/os/input_event.h | 4 | ||||
-rw-r--r-- | scene/gui/input_action.cpp | 2 |
3 files changed, 44 insertions, 1 deletions
diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index dbdf9628e3..e60f588be3 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -89,6 +89,11 @@ bool InputEvent::action_match(const Ref<InputEvent> &p_event) const { return false; } +bool InputEvent::shortcut_match(const Ref<InputEvent> &p_event) const { + + return false; +} + bool InputEvent::is_action_type() const { return false; @@ -130,6 +135,7 @@ void InputEvent::_bind_methods() { ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text); ClassDB::bind_method(D_METHOD("action_match", "event:InputEvent"), &InputEvent::action_match); + ClassDB::bind_method(D_METHOD("shortcut_match", "event:InputEvent"), &InputEvent::shortcut_match); ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type); @@ -276,6 +282,27 @@ uint32_t InputEventKey::get_scancode_with_modifiers() const { return sc; } +String InputEventKey::as_text() const { + + String kc = keycode_get_string(scancode); + if (kc == String()) + return kc; + + if (get_metakey()) { + kc = "Meta+" + kc; + } + if (get_alt()) { + kc = "Alt+" + kc; + } + if (get_shift()) { + kc = "Shift+" + kc; + } + if (get_control()) { + kc = "Ctrl+" + kc; + } + return kc; +} + bool InputEventKey::action_match(const Ref<InputEvent> &p_event) const { Ref<InputEventKey> key = p_event; @@ -288,6 +315,18 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event) const { return get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code); } +bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const { + + Ref<InputEventKey> key = p_event; + if (key.is_null()) + return false; + + uint32_t code = get_scancode_with_modifiers(); + uint32_t event_code = key->get_scancode_with_modifiers(); + + return code == event_code; +} + void InputEventKey::_bind_methods() { ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed); diff --git a/core/os/input_event.h b/core/os/input_event.h index 6a694df345..b120d4b840 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -165,6 +165,7 @@ public: virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const; virtual bool action_match(const Ref<InputEvent> &p_event) const; + virtual bool shortcut_match(const Ref<InputEvent> &p_event) const; virtual bool is_action_type() const; InputEvent(); @@ -243,9 +244,12 @@ public: uint32_t get_scancode_with_modifiers() const; virtual bool action_match(const Ref<InputEvent> &p_event) const; + virtual bool shortcut_match(const Ref<InputEvent> &p_event) const; virtual bool is_action_type() const { return true; } + virtual String as_text() const; + InputEventKey(); }; diff --git a/scene/gui/input_action.cpp b/scene/gui/input_action.cpp index 311cb4ab13..3f80c31c8b 100644 --- a/scene/gui/input_action.cpp +++ b/scene/gui/input_action.cpp @@ -43,7 +43,7 @@ Ref<InputEvent> ShortCut::get_shortcut() const { bool ShortCut::is_shortcut(const Ref<InputEvent> &p_event) const { - return shortcut.is_valid() && shortcut->action_match(p_event); + return shortcut.is_valid() && shortcut->shortcut_match(p_event); } String ShortCut::get_as_text() const { |