summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorbruvzg <7645683+bruvzg@users.noreply.github.com>2018-04-05 20:59:35 +0300
committerbruvzg <7645683+bruvzg@users.noreply.github.com>2020-02-25 12:30:33 +0200
commit1af06d3d4608b17c74caed951cd9579ccbba229d (patch)
treea0d57c6cd3c00614a5a30290085e91a6097e78b6 /core
parent376a8255a9b65b016fcd81634bfd54fb7fa029df (diff)
Rename `scancode` to `keycode`.
Add `physical_keycode` (keyboard layout independent keycodes) to InputEventKey and InputMap. Fix non-latin keyboard layout keycodes on Linux/X11 (fallback to physical keycodes).
Diffstat (limited to 'core')
-rw-r--r--core/bind/core_bind.cpp14
-rw-r--r--core/bind/core_bind.h6
-rw-r--r--core/input_map.cpp30
-rw-r--r--core/os/input.cpp2
-rw-r--r--core/os/input.h2
-rw-r--r--core/os/input_event.cpp76
-rw-r--r--core/os/input_event.h13
-rw-r--r--core/project_settings.cpp30
8 files changed, 111 insertions, 62 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp
index f5a351f16a..fda97ed9e3 100644
--- a/core/bind/core_bind.cpp
+++ b/core/bind/core_bind.cpp
@@ -1113,15 +1113,17 @@ String _OS::get_system_dir(SystemDir p_dir) const {
return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
}
-String _OS::get_scancode_string(uint32_t p_code) const {
+String _OS::get_keycode_string(uint32_t p_code) const {
return keycode_get_string(p_code);
}
-bool _OS::is_scancode_unicode(uint32_t p_unicode) const {
+
+bool _OS::is_keycode_unicode(uint32_t p_unicode) const {
return keycode_has_unicode(p_unicode);
}
-int _OS::find_scancode_from_string(const String &p_code) const {
+
+int _OS::find_keycode_from_string(const String &p_code) const {
return find_keycode(p_code);
}
@@ -1312,9 +1314,9 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("native_video_pause"), &_OS::native_video_pause);
ClassDB::bind_method(D_METHOD("native_video_unpause"), &_OS::native_video_unpause);
- ClassDB::bind_method(D_METHOD("get_scancode_string", "code"), &_OS::get_scancode_string);
- ClassDB::bind_method(D_METHOD("is_scancode_unicode", "code"), &_OS::is_scancode_unicode);
- ClassDB::bind_method(D_METHOD("find_scancode_from_string", "string"), &_OS::find_scancode_from_string);
+ ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &_OS::get_keycode_string);
+ ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &_OS::is_keycode_unicode);
+ ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &_OS::find_keycode_from_string);
ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h
index ae569ea189..85dc00a968 100644
--- a/core/bind/core_bind.h
+++ b/core/bind/core_bind.h
@@ -253,9 +253,9 @@ public:
String get_unique_id() const;
- String get_scancode_string(uint32_t p_code) const;
- bool is_scancode_unicode(uint32_t p_unicode) const;
- int find_scancode_from_string(const String &p_code) const;
+ String get_keycode_string(uint32_t p_code) const;
+ bool is_keycode_unicode(uint32_t p_unicode) const;
+ int find_keycode_from_string(const String &p_code) const;
void set_use_file_access_save_and_swap(bool p_enable);
diff --git a/core/input_map.cpp b/core/input_map.cpp
index 36a0e88ae0..b855e14e0d 100644
--- a/core/input_map.cpp
+++ b/core/input_map.cpp
@@ -256,76 +256,76 @@ void InputMap::load_default() {
add_action("ui_accept");
key.instance();
- key->set_scancode(KEY_ENTER);
+ key->set_keycode(KEY_ENTER);
action_add_event("ui_accept", key);
key.instance();
- key->set_scancode(KEY_KP_ENTER);
+ key->set_keycode(KEY_KP_ENTER);
action_add_event("ui_accept", key);
key.instance();
- key->set_scancode(KEY_SPACE);
+ key->set_keycode(KEY_SPACE);
action_add_event("ui_accept", key);
add_action("ui_select");
key.instance();
- key->set_scancode(KEY_SPACE);
+ key->set_keycode(KEY_SPACE);
action_add_event("ui_select", key);
add_action("ui_cancel");
key.instance();
- key->set_scancode(KEY_ESCAPE);
+ key->set_keycode(KEY_ESCAPE);
action_add_event("ui_cancel", key);
add_action("ui_focus_next");
key.instance();
- key->set_scancode(KEY_TAB);
+ key->set_keycode(KEY_TAB);
action_add_event("ui_focus_next", key);
add_action("ui_focus_prev");
key.instance();
- key->set_scancode(KEY_TAB);
+ key->set_keycode(KEY_TAB);
key->set_shift(true);
action_add_event("ui_focus_prev", key);
add_action("ui_left");
key.instance();
- key->set_scancode(KEY_LEFT);
+ key->set_keycode(KEY_LEFT);
action_add_event("ui_left", key);
add_action("ui_right");
key.instance();
- key->set_scancode(KEY_RIGHT);
+ key->set_keycode(KEY_RIGHT);
action_add_event("ui_right", key);
add_action("ui_up");
key.instance();
- key->set_scancode(KEY_UP);
+ key->set_keycode(KEY_UP);
action_add_event("ui_up", key);
add_action("ui_down");
key.instance();
- key->set_scancode(KEY_DOWN);
+ key->set_keycode(KEY_DOWN);
action_add_event("ui_down", key);
add_action("ui_page_up");
key.instance();
- key->set_scancode(KEY_PAGEUP);
+ key->set_keycode(KEY_PAGEUP);
action_add_event("ui_page_up", key);
add_action("ui_page_down");
key.instance();
- key->set_scancode(KEY_PAGEDOWN);
+ key->set_keycode(KEY_PAGEDOWN);
action_add_event("ui_page_down", key);
add_action("ui_home");
key.instance();
- key->set_scancode(KEY_HOME);
+ key->set_keycode(KEY_HOME);
action_add_event("ui_home", key);
add_action("ui_end");
key.instance();
- key->set_scancode(KEY_END);
+ key->set_keycode(KEY_END);
action_add_event("ui_end", key);
//set("display/window/handheld/orientation", "landscape");
diff --git a/core/os/input.cpp b/core/os/input.cpp
index 6f0392fec9..1768b851df 100644
--- a/core/os/input.cpp
+++ b/core/os/input.cpp
@@ -57,7 +57,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_key_pressed", "keycode"), &Input::is_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"), &Input::is_action_pressed);
diff --git a/core/os/input.h b/core/os/input.h
index 8df3b1c5a9..55e0511080 100644
--- a/core/os/input.h
+++ b/core/os/input.h
@@ -79,7 +79,7 @@ public:
static Input *get_singleton();
- virtual bool is_key_pressed(int p_scancode) const = 0;
+ virtual bool is_key_pressed(int p_keycode) 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) const = 0;
diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp
index 7d1ae872f4..6ab306462f 100644
--- a/core/os/input_event.cpp
+++ b/core/os/input_event.cpp
@@ -237,19 +237,31 @@ bool InputEventKey::is_pressed() const {
return pressed;
}
-void InputEventKey::set_scancode(uint32_t p_scancode) {
+void InputEventKey::set_keycode(uint32_t p_keycode) {
- scancode = p_scancode;
+ keycode = p_keycode;
}
-uint32_t InputEventKey::get_scancode() const {
- return scancode;
+uint32_t InputEventKey::get_keycode() const {
+
+ return keycode;
+}
+
+void InputEventKey::set_physical_keycode(uint32_t p_keycode) {
+
+ physical_keycode = p_keycode;
+}
+
+uint32_t InputEventKey::get_physical_keycode() const {
+
+ return physical_keycode;
}
void InputEventKey::set_unicode(uint32_t p_unicode) {
unicode = p_unicode;
}
+
uint32_t InputEventKey::get_unicode() const {
return unicode;
@@ -259,14 +271,30 @@ void InputEventKey::set_echo(bool p_enable) {
echo = p_enable;
}
+
bool InputEventKey::is_echo() const {
return echo;
}
-uint32_t InputEventKey::get_scancode_with_modifiers() const {
+uint32_t InputEventKey::get_keycode_with_modifiers() const {
- uint32_t sc = scancode;
+ uint32_t sc = keycode;
+ if (get_control())
+ sc |= KEY_MASK_CTRL;
+ if (get_alt())
+ sc |= KEY_MASK_ALT;
+ if (get_shift())
+ sc |= KEY_MASK_SHIFT;
+ if (get_metakey())
+ sc |= KEY_MASK_META;
+
+ return sc;
+}
+
+uint32_t InputEventKey::get_physical_keycode_with_modifiers() const {
+
+ uint32_t sc = physical_keycode;
if (get_control())
sc |= KEY_MASK_CTRL;
if (get_alt())
@@ -281,7 +309,7 @@ uint32_t InputEventKey::get_scancode_with_modifiers() const {
String InputEventKey::as_text() const {
- String kc = keycode_get_string(scancode);
+ String kc = keycode_get_string(keycode);
if (kc == String())
return kc;
@@ -306,10 +334,18 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed
if (key.is_null())
return false;
- uint32_t code = get_scancode_with_modifiers();
- uint32_t event_code = key->get_scancode_with_modifiers();
+ bool match = false;
+ if (get_keycode() == 0) {
+ uint32_t code = get_physical_keycode_with_modifiers();
+ uint32_t event_code = key->get_physical_keycode_with_modifiers();
- bool match = get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code);
+ match = get_physical_keycode() == key->get_physical_keycode() && (!key->is_pressed() || (code & event_code) == code);
+ } else {
+ uint32_t code = get_keycode_with_modifiers();
+ uint32_t event_code = key->get_keycode_with_modifiers();
+
+ match = get_keycode() == key->get_keycode() && (!key->is_pressed() || (code & event_code) == code);
+ }
if (match) {
if (p_pressed != NULL)
*p_pressed = key->is_pressed();
@@ -325,8 +361,8 @@ bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const {
if (key.is_null())
return false;
- uint32_t code = get_scancode_with_modifiers();
- uint32_t event_code = key->get_scancode_with_modifiers();
+ uint32_t code = get_keycode_with_modifiers();
+ uint32_t event_code = key->get_keycode_with_modifiers();
return code == event_code;
}
@@ -335,18 +371,23 @@ void InputEventKey::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventKey::set_pressed);
- ClassDB::bind_method(D_METHOD("set_scancode", "scancode"), &InputEventKey::set_scancode);
- ClassDB::bind_method(D_METHOD("get_scancode"), &InputEventKey::get_scancode);
+ ClassDB::bind_method(D_METHOD("set_keycode", "keycode"), &InputEventKey::set_keycode);
+ ClassDB::bind_method(D_METHOD("get_keycode"), &InputEventKey::get_keycode);
+
+ ClassDB::bind_method(D_METHOD("set_physical_keycode", "physical_keycode"), &InputEventKey::set_physical_keycode);
+ ClassDB::bind_method(D_METHOD("get_physical_keycode"), &InputEventKey::get_physical_keycode);
ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode);
ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode);
ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo);
- ClassDB::bind_method(D_METHOD("get_scancode_with_modifiers"), &InputEventKey::get_scancode_with_modifiers);
+ ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers);
+ ClassDB::bind_method(D_METHOD("get_physical_keycode_with_modifiers"), &InputEventKey::get_physical_keycode_with_modifiers);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "scancode"), "set_scancode", "get_scancode");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "echo"), "set_echo", "is_echo");
}
@@ -354,7 +395,8 @@ void InputEventKey::_bind_methods() {
InputEventKey::InputEventKey() {
pressed = false;
- scancode = 0;
+ keycode = 0;
+ physical_keycode = 0;
unicode = 0; ///unicode
echo = false;
}
diff --git a/core/os/input_event.h b/core/os/input_event.h
index c6b04bcfa5..c105fcd1c1 100644
--- a/core/os/input_event.h
+++ b/core/os/input_event.h
@@ -256,7 +256,8 @@ class InputEventKey : public InputEventWithModifiers {
bool pressed; /// otherwise release
- uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
+ uint32_t keycode; ///< check keyboard.h , KeyCode enum, without modifier masks
+ uint32_t physical_keycode;
uint32_t unicode; ///unicode
bool echo; /// true if this is an echo key
@@ -268,8 +269,11 @@ public:
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;
- void set_scancode(uint32_t p_scancode);
- uint32_t get_scancode() const;
+ void set_keycode(uint32_t p_keycode);
+ uint32_t get_keycode() const;
+
+ void set_physical_keycode(uint32_t p_keycode);
+ uint32_t get_physical_keycode() const;
void set_unicode(uint32_t p_unicode);
uint32_t get_unicode() const;
@@ -277,7 +281,8 @@ public:
void set_echo(bool p_enable);
virtual bool is_echo() const;
- uint32_t get_scancode_with_modifiers() const;
+ uint32_t get_keycode_with_modifiers() const;
+ uint32_t get_physical_keycode_with_modifiers() const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
diff --git a/core/project_settings.cpp b/core/project_settings.cpp
index 90487bda0d..cf6b0471ec 100644
--- a/core/project_settings.cpp
+++ b/core/project_settings.cpp
@@ -1039,13 +1039,13 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_ENTER);
+ key->set_keycode(KEY_ENTER);
events.push_back(key);
key.instance();
- key->set_scancode(KEY_KP_ENTER);
+ key->set_keycode(KEY_KP_ENTER);
events.push_back(key);
key.instance();
- key->set_scancode(KEY_SPACE);
+ key->set_keycode(KEY_SPACE);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_0);
@@ -1058,7 +1058,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_SPACE);
+ key->set_keycode(KEY_SPACE);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_3);
@@ -1071,7 +1071,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_ESCAPE);
+ key->set_keycode(KEY_ESCAPE);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_BUTTON_1);
@@ -1084,7 +1084,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_TAB);
+ key->set_keycode(KEY_TAB);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_focus_next", action);
@@ -1094,7 +1094,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_TAB);
+ key->set_keycode(KEY_TAB);
key->set_shift(true);
events.push_back(key);
action["events"] = events;
@@ -1105,7 +1105,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_LEFT);
+ key->set_keycode(KEY_LEFT);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_DPAD_LEFT);
@@ -1118,7 +1118,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_RIGHT);
+ key->set_keycode(KEY_RIGHT);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_DPAD_RIGHT);
@@ -1131,7 +1131,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_UP);
+ key->set_keycode(KEY_UP);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_DPAD_UP);
@@ -1144,7 +1144,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_DOWN);
+ key->set_keycode(KEY_DOWN);
events.push_back(key);
joyb.instance();
joyb->set_button_index(JOY_DPAD_DOWN);
@@ -1157,7 +1157,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_PAGEUP);
+ key->set_keycode(KEY_PAGEUP);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_page_up", action);
@@ -1167,7 +1167,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_PAGEDOWN);
+ key->set_keycode(KEY_PAGEDOWN);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_page_down", action);
@@ -1177,7 +1177,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_HOME);
+ key->set_keycode(KEY_HOME);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_home", action);
@@ -1187,7 +1187,7 @@ ProjectSettings::ProjectSettings() {
action["deadzone"] = Variant(0.5f);
events = Array();
key.instance();
- key->set_scancode(KEY_END);
+ key->set_keycode(KEY_END);
events.push_back(key);
action["events"] = events;
GLOBAL_DEF("input/ui_end", action);