Skip to content

Commit 1e84bc4

Browse files
committed
Merge pull request godotengine#108260 from Silver1063/master
Fix modifier order in keycode string generation
2 parents 8b4b93a + 00f5b23 commit 1e84bc4

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

core/input/input_event.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ String InputEventWithModifiers::as_text() const {
260260
if (is_ctrl_pressed()) {
261261
mod_names.push_back(find_keycode_name(Key::CTRL));
262262
}
263-
if (is_shift_pressed()) {
264-
mod_names.push_back(find_keycode_name(Key::SHIFT));
265-
}
266263
if (is_alt_pressed()) {
267264
mod_names.push_back(find_keycode_name(Key::ALT));
268265
}
266+
if (is_shift_pressed()) {
267+
mod_names.push_back(find_keycode_name(Key::SHIFT));
268+
}
269269
if (is_meta_pressed()) {
270270
mod_names.push_back(find_keycode_name(Key::META));
271271
}

core/os/keyboard.cpp

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -360,51 +360,45 @@ bool keycode_has_unicode(Key p_keycode) {
360360
}
361361

362362
String keycode_get_string(Key p_code) {
363-
String codestr;
364-
if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) {
365-
codestr += find_keycode_name(Key::SHIFT);
366-
codestr += "+";
363+
Vector<String> keycode_string;
364+
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"))) {
365+
keycode_string.push_back(find_keycode_name(Key::CTRL));
366+
}
367+
if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
368+
keycode_string.push_back(find_keycode_name(Key::CTRL));
367369
}
368370
if ((p_code & KeyModifierMask::ALT) != Key::NONE) {
369-
codestr += find_keycode_name(Key::ALT);
370-
codestr += "+";
371+
keycode_string.push_back(find_keycode_name(Key::ALT));
371372
}
372-
if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
373-
if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
374-
codestr += find_keycode_name(Key::META);
375-
} else {
376-
codestr += find_keycode_name(Key::CTRL);
377-
}
378-
codestr += "+";
373+
if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) {
374+
keycode_string.push_back(find_keycode_name(Key::SHIFT));
379375
}
380-
if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
381-
codestr += find_keycode_name(Key::CTRL);
382-
codestr += "+";
376+
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"))) {
377+
keycode_string.push_back(find_keycode_name(Key::META));
383378
}
384379
if ((p_code & KeyModifierMask::META) != Key::NONE) {
385-
codestr += find_keycode_name(Key::META);
386-
codestr += "+";
380+
keycode_string.push_back(find_keycode_name(Key::META));
387381
}
388382

389383
p_code &= KeyModifierMask::CODE_MASK;
390384
if ((char32_t)p_code == 0) {
391385
// The key was just a modifier without any code.
392-
return codestr;
386+
return String("+").join(keycode_string);
393387
}
394388

389+
// The key is a named keycode.
395390
const _KeyCodeText *kct = &_keycodes[0];
396-
397391
while (kct->text) {
398392
if (kct->code == p_code) {
399-
codestr += kct->text;
400-
return codestr;
393+
keycode_string.push_back(kct->text);
394+
return String("+").join(keycode_string);
401395
}
402396
kct++;
403397
}
404398

405-
codestr += String::chr((char32_t)p_code);
406-
407-
return codestr;
399+
// The key is a single character.
400+
keycode_string.push_back(String::chr((char32_t)p_code));
401+
return String("+").join(keycode_string);
408402
}
409403

410404
Key find_keycode(const String &p_codestr) {

tests/core/input/test_input_event_key.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
125125
none_key.set_physical_keycode(Key::ENTER);
126126
CHECK(none_key.as_text() == "Ctrl+Enter (Physical)");
127127

128+
// Key is None WITH a physical key AND multiple modifiers, checks for correct ordering.
129+
none_key.set_alt_pressed(true);
130+
none_key.set_shift_pressed(true);
131+
#ifdef MACOS_ENABLED
132+
CHECK(none_key.as_text() != "Ctrl+Shift+Option+Enter (Physical)");
133+
CHECK(none_key.as_text() == "Ctrl+Option+Shift+Enter (Physical)");
134+
#else
135+
CHECK(none_key.as_text() != "Ctrl+Shift+Alt+Enter (Physical)");
136+
CHECK(none_key.as_text() == "Ctrl+Alt+Shift+Enter (Physical)");
137+
#endif
138+
128139
InputEventKey none_key2;
129140

130141
// Key is None without modifiers with a physical key.
@@ -145,6 +156,17 @@ TEST_CASE("[InputEventKey] Key correctly converts itself to text") {
145156
CHECK(key.as_text() != "Space");
146157
CHECK(key.as_text() == "Ctrl+Space");
147158

159+
// Key has keycode and multiple modifiers, checks for correct ordering.
160+
key.set_alt_pressed(true);
161+
key.set_shift_pressed(true);
162+
#ifdef MACOS_ENABLED
163+
CHECK(key.as_text() != "Ctrl+Shift+Option+Space");
164+
CHECK(key.as_text() == "Ctrl+Option+Shift+Space");
165+
#else
166+
CHECK(key.as_text() != "Ctrl+Shift+Alt+Space");
167+
CHECK(key.as_text() == "Ctrl+Alt+Shift+Space");
168+
#endif
169+
148170
// Since the keycode is set to Key::NONE upon initialization of the
149171
// InputEventKey and you can only update it with another Key, the keycode
150172
// cannot be empty, so the kc.is_empty() case cannot be tested.

0 commit comments

Comments
 (0)