summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/core_constants.cpp3
-rw-r--r--core/input/input.cpp2
-rw-r--r--core/input/input.h3
-rw-r--r--core/input/input_event.cpp10
-rw-r--r--core/input/input_event.h15
-rw-r--r--core/os/keyboard.h47
-rw-r--r--core/variant/binder_common.h3
7 files changed, 66 insertions, 17 deletions
diff --git a/core/core_constants.cpp b/core/core_constants.cpp
index b09e78d653..5791f79053 100644
--- a/core/core_constants.cpp
+++ b/core/core_constants.cpp
@@ -104,9 +104,6 @@ static Vector<_CoreConstant> _global_constants;
#endif
-VARIANT_ENUM_CAST(Key);
-VARIANT_ENUM_CAST(KeyModifierMask);
-
void register_global_constants() {
BIND_CORE_ENUM_CONSTANT(SIDE_LEFT);
BIND_CORE_ENUM_CONSTANT(SIDE_TOP);
diff --git a/core/input/input.cpp b/core/input/input.cpp
index c205726b0a..2da50b7dff 100644
--- a/core/input/input.cpp
+++ b/core/input/input.cpp
@@ -221,7 +221,7 @@ Input::SpeedTrack::SpeedTrack() {
reset();
}
-bool Input::is_key_pressed(int p_keycode) const {
+bool Input::is_key_pressed(Key p_keycode) const {
_THREAD_SAFE_METHOD_
return keys_pressed.has(p_keycode);
}
diff --git a/core/input/input.h b/core/input/input.h
index fbcd5836ea..ee991aa725 100644
--- a/core/input/input.h
+++ b/core/input/input.h
@@ -33,6 +33,7 @@
#include "core/input/input_event.h"
#include "core/object/object.h"
+#include "core/os/keyboard.h"
#include "core/os/thread_safe.h"
class Input : public Object {
@@ -244,7 +245,7 @@ public:
static Input *get_singleton();
- bool is_key_pressed(int p_keycode) const;
+ bool is_key_pressed(Key p_keycode) const;
bool is_mouse_button_pressed(MouseButton p_button) const;
bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp
index 1715fca9e8..6e5c1a58ae 100644
--- a/core/input/input_event.cpp
+++ b/core/input/input_event.cpp
@@ -307,21 +307,21 @@ bool InputEventKey::is_pressed() const {
return pressed;
}
-void InputEventKey::set_keycode(uint32_t p_keycode) {
+void InputEventKey::set_keycode(Key p_keycode) {
keycode = p_keycode;
emit_changed();
}
-uint32_t InputEventKey::get_keycode() const {
+Key InputEventKey::get_keycode() const {
return keycode;
}
-void InputEventKey::set_physical_keycode(uint32_t p_keycode) {
+void InputEventKey::set_physical_keycode(Key p_keycode) {
physical_keycode = p_keycode;
emit_changed();
}
-uint32_t InputEventKey::get_physical_keycode() const {
+Key InputEventKey::get_physical_keycode() const {
return physical_keycode;
}
@@ -387,7 +387,7 @@ String InputEventKey::to_string() {
return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, pressed=%s, echo=%s", kc, mods, physical, p, e);
}
-Ref<InputEventKey> InputEventKey::create_reference(uint32_t p_keycode) {
+Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode) {
Ref<InputEventKey> ie;
ie.instantiate();
ie->set_keycode(p_keycode & KEY_CODE_MASK);
diff --git a/core/input/input_event.h b/core/input/input_event.h
index 98d204fe13..57b6091123 100644
--- a/core/input/input_event.h
+++ b/core/input/input_event.h
@@ -34,6 +34,7 @@
#include "core/input/input_enums.h"
#include "core/io/resource.h"
#include "core/math/transform_2d.h"
+#include "core/os/keyboard.h"
#include "core/string/ustring.h"
#include "core/typedefs.h"
@@ -163,8 +164,8 @@ class InputEventKey : public InputEventWithModifiers {
bool pressed = false; /// otherwise release
- uint32_t keycode = 0; ///< check keyboard.h , KeyCode enum, without modifier masks
- uint32_t physical_keycode = 0;
+ Key keycode = KEY_NONE; // Key enum, without modifier masks.
+ Key physical_keycode = KEY_NONE;
uint32_t unicode = 0; ///unicode
bool echo = false; /// true if this is an echo key
@@ -176,11 +177,11 @@ public:
void set_pressed(bool p_pressed);
virtual bool is_pressed() const override;
- void set_keycode(uint32_t p_keycode);
- uint32_t get_keycode() const;
+ void set_keycode(Key p_keycode);
+ Key get_keycode() const;
- void set_physical_keycode(uint32_t p_keycode);
- uint32_t get_physical_keycode() const;
+ void set_physical_keycode(Key p_keycode);
+ Key get_physical_keycode() const;
void set_unicode(uint32_t p_unicode);
uint32_t get_unicode() const;
@@ -199,7 +200,7 @@ public:
virtual String as_text() const override;
virtual String to_string() override;
- static Ref<InputEventKey> create_reference(uint32_t p_keycode_with_modifier_masks);
+ static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks);
InputEventKey() {}
};
diff --git a/core/os/keyboard.h b/core/os/keyboard.h
index 33f9213c4e..52174432d9 100644
--- a/core/os/keyboard.h
+++ b/core/os/keyboard.h
@@ -46,6 +46,7 @@ enum {
};
enum Key {
+ KEY_NONE = 0,
/* CURSOR/FUNCTION/BROWSER/MULTIMEDIA/MISC KEYS */
KEY_ESCAPE = SPKEY | 0x01,
KEY_TAB = SPKEY | 0x02,
@@ -314,6 +315,52 @@ enum KeyModifierMask {
// bit 31 can't be used because variant uses regular 32 bits int as datatype
};
+// To avoid having unnecessary operators, only define the ones that are needed.
+
+inline Key operator-(uint32_t a, Key b) {
+ return (Key)(a - (uint32_t)b);
+}
+
+inline Key &operator-=(Key &a, int b) {
+ return (Key &)((int &)a -= b);
+}
+
+inline Key operator+(Key a, Key b) {
+ return (Key)((int)a - (int)b);
+}
+
+inline Key &operator|=(Key &a, Key b) {
+ return (Key &)((int &)a |= (int)b);
+}
+
+inline Key &operator|=(Key &a, KeyModifierMask b) {
+ return (Key &)((int &)a |= (int)b);
+}
+
+inline Key operator|(Key a, KeyModifierMask b) {
+ return (Key)((int)a | (int)b);
+}
+
+inline Key operator&(Key a, KeyModifierMask b) {
+ return (Key)((int)a & (int)b);
+}
+
+inline Key operator+(KeyModifierMask a, Key b) {
+ return (Key)((int)a + (int)b);
+}
+
+inline Key operator|(KeyModifierMask a, Key b) {
+ return (Key)((int)a | (int)b);
+}
+
+inline KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) {
+ return (KeyModifierMask)((int)a + (int)b);
+}
+
+inline KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) {
+ return (KeyModifierMask)((int)a | (int)b);
+}
+
String keycode_get_string(uint32_t p_code);
bool keycode_has_unicode(uint32_t p_keycode);
int find_keycode(const String &p_code);
diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h
index 001da3ddcb..3b2c837096 100644
--- a/core/variant/binder_common.h
+++ b/core/variant/binder_common.h
@@ -33,6 +33,7 @@
#include "core/input/input_enums.h"
#include "core/object/object.h"
+#include "core/os/keyboard.h"
#include "core/templates/list.h"
#include "core/templates/simple_type.h"
#include "core/typedefs.h"
@@ -96,6 +97,8 @@ VARIANT_ENUM_CAST(HatDir);
VARIANT_ENUM_CAST(HatMask);
VARIANT_ENUM_CAST(JoyAxis);
VARIANT_ENUM_CAST(JoyButton);
+VARIANT_ENUM_CAST(Key);
+VARIANT_ENUM_CAST(KeyModifierMask);
VARIANT_ENUM_CAST(MIDIMessage);
VARIANT_ENUM_CAST(MouseButton);
VARIANT_ENUM_CAST(Orientation);