You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Fix modifier order in keycode string generation
Fix the order in which modifier keys are appended in as_text() and keycode_get_string() to ensure consistent and logical ordering (Ctrl, Alt, Shift, Meta). Refactored keycode_get_string() to use a vector for building the key string, improving readability and maintainability.
This commit is contained in:
@@ -256,12 +256,12 @@ String InputEventWithModifiers::as_text() const {
|
|||||||
if (is_ctrl_pressed()) {
|
if (is_ctrl_pressed()) {
|
||||||
mod_names.push_back(find_keycode_name(Key::CTRL));
|
mod_names.push_back(find_keycode_name(Key::CTRL));
|
||||||
}
|
}
|
||||||
if (is_shift_pressed()) {
|
|
||||||
mod_names.push_back(find_keycode_name(Key::SHIFT));
|
|
||||||
}
|
|
||||||
if (is_alt_pressed()) {
|
if (is_alt_pressed()) {
|
||||||
mod_names.push_back(find_keycode_name(Key::ALT));
|
mod_names.push_back(find_keycode_name(Key::ALT));
|
||||||
}
|
}
|
||||||
|
if (is_shift_pressed()) {
|
||||||
|
mod_names.push_back(find_keycode_name(Key::SHIFT));
|
||||||
|
}
|
||||||
if (is_meta_pressed()) {
|
if (is_meta_pressed()) {
|
||||||
mod_names.push_back(find_keycode_name(Key::META));
|
mod_names.push_back(find_keycode_name(Key::META));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,51 +360,45 @@ bool keycode_has_unicode(Key p_keycode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String keycode_get_string(Key p_code) {
|
String keycode_get_string(Key p_code) {
|
||||||
String codestr;
|
Vector<String> keycode_string;
|
||||||
if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) {
|
if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE && !(OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios"))) {
|
||||||
codestr += find_keycode_name(Key::SHIFT);
|
keycode_string.push_back(find_keycode_name(Key::CTRL));
|
||||||
codestr += "+";
|
|
||||||
}
|
|
||||||
if ((p_code & KeyModifierMask::ALT) != Key::NONE) {
|
|
||||||
codestr += find_keycode_name(Key::ALT);
|
|
||||||
codestr += "+";
|
|
||||||
}
|
|
||||||
if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
|
|
||||||
if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
|
|
||||||
codestr += find_keycode_name(Key::META);
|
|
||||||
} else {
|
|
||||||
codestr += find_keycode_name(Key::CTRL);
|
|
||||||
}
|
|
||||||
codestr += "+";
|
|
||||||
}
|
}
|
||||||
if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
|
if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
|
||||||
codestr += find_keycode_name(Key::CTRL);
|
keycode_string.push_back(find_keycode_name(Key::CTRL));
|
||||||
codestr += "+";
|
}
|
||||||
|
if ((p_code & KeyModifierMask::ALT) != Key::NONE) {
|
||||||
|
keycode_string.push_back(find_keycode_name(Key::ALT));
|
||||||
|
}
|
||||||
|
if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) {
|
||||||
|
keycode_string.push_back(find_keycode_name(Key::SHIFT));
|
||||||
|
}
|
||||||
|
if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE && (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios"))) {
|
||||||
|
keycode_string.push_back(find_keycode_name(Key::META));
|
||||||
}
|
}
|
||||||
if ((p_code & KeyModifierMask::META) != Key::NONE) {
|
if ((p_code & KeyModifierMask::META) != Key::NONE) {
|
||||||
codestr += find_keycode_name(Key::META);
|
keycode_string.push_back(find_keycode_name(Key::META));
|
||||||
codestr += "+";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p_code &= KeyModifierMask::CODE_MASK;
|
p_code &= KeyModifierMask::CODE_MASK;
|
||||||
if ((char32_t)p_code == 0) {
|
if ((char32_t)p_code == 0) {
|
||||||
// The key was just a modifier without any code.
|
// The key was just a modifier without any code.
|
||||||
return codestr;
|
return String("+").join(keycode_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The key is a named keycode.
|
||||||
const _KeyCodeText *kct = &_keycodes[0];
|
const _KeyCodeText *kct = &_keycodes[0];
|
||||||
|
|
||||||
while (kct->text) {
|
while (kct->text) {
|
||||||
if (kct->code == p_code) {
|
if (kct->code == p_code) {
|
||||||
codestr += kct->text;
|
keycode_string.push_back(kct->text);
|
||||||
return codestr;
|
return String("+").join(keycode_string);
|
||||||
}
|
}
|
||||||
kct++;
|
kct++;
|
||||||
}
|
}
|
||||||
|
|
||||||
codestr += String::chr((char32_t)p_code);
|
// The key is a single character.
|
||||||
|
keycode_string.push_back(String::chr((char32_t)p_code));
|
||||||
return codestr;
|
return String("+").join(keycode_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
Key find_keycode(const String &p_codestr) {
|
Key find_keycode(const String &p_codestr) {
|
||||||
|
|||||||
@@ -125,6 +125,17 @@ TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
|
|||||||
none_key.set_physical_keycode(Key::ENTER);
|
none_key.set_physical_keycode(Key::ENTER);
|
||||||
CHECK(none_key.as_text() == "Ctrl+Enter (Physical)");
|
CHECK(none_key.as_text() == "Ctrl+Enter (Physical)");
|
||||||
|
|
||||||
|
// Key is None WITH a physical key AND multiple modifiers, checks for correct ordering.
|
||||||
|
none_key.set_alt_pressed(true);
|
||||||
|
none_key.set_shift_pressed(true);
|
||||||
|
#ifdef MACOS_ENABLED
|
||||||
|
CHECK(none_key.as_text() != "Ctrl+Shift+Option+Enter (Physical)");
|
||||||
|
CHECK(none_key.as_text() == "Ctrl+Option+Shift+Enter (Physical)");
|
||||||
|
#else
|
||||||
|
CHECK(none_key.as_text() != "Ctrl+Shift+Alt+Enter (Physical)");
|
||||||
|
CHECK(none_key.as_text() == "Ctrl+Alt+Shift+Enter (Physical)");
|
||||||
|
#endif
|
||||||
|
|
||||||
InputEventKey none_key2;
|
InputEventKey none_key2;
|
||||||
|
|
||||||
// Key is None without modifiers with a physical key.
|
// Key is None without modifiers with a physical key.
|
||||||
@@ -145,6 +156,17 @@ TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
|
|||||||
CHECK(key.as_text() != "Space");
|
CHECK(key.as_text() != "Space");
|
||||||
CHECK(key.as_text() == "Ctrl+Space");
|
CHECK(key.as_text() == "Ctrl+Space");
|
||||||
|
|
||||||
|
// Key has keycode and multiple modifiers, checks for correct ordering.
|
||||||
|
key.set_alt_pressed(true);
|
||||||
|
key.set_shift_pressed(true);
|
||||||
|
#ifdef MACOS_ENABLED
|
||||||
|
CHECK(key.as_text() != "Ctrl+Shift+Option+Space");
|
||||||
|
CHECK(key.as_text() == "Ctrl+Option+Shift+Space");
|
||||||
|
#else
|
||||||
|
CHECK(key.as_text() != "Ctrl+Shift+Alt+Space");
|
||||||
|
CHECK(key.as_text() == "Ctrl+Alt+Shift+Space");
|
||||||
|
#endif
|
||||||
|
|
||||||
// Since the keycode is set to Key::NONE upon initialization of the
|
// Since the keycode is set to Key::NONE upon initialization of the
|
||||||
// InputEventKey and you can only update it with another Key, the keycode
|
// InputEventKey and you can only update it with another Key, the keycode
|
||||||
// cannot be empty, so the kc.is_empty() case cannot be tested.
|
// cannot be empty, so the kc.is_empty() case cannot be tested.
|
||||||
|
|||||||
Reference in New Issue
Block a user