Skip to content

Commit 297650a

Browse files
committed
Merge pull request godotengine#93466 from bruvzg/alt_oem
[TextEdit / LineEdit] Add support for OEM Alt codes input.
2 parents 31bb3be + aa0ade5 commit 297650a

File tree

7 files changed

+257
-25
lines changed

7 files changed

+257
-25
lines changed

core/string/char_range.inc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,3 +3627,43 @@ constexpr inline CharRange unicode_letter[] = {
36273627
{ 0x30000, 0x3134a },
36283628
{ 0x31350, 0x323af },
36293629
};
3630+
3631+
// OEM encodings for Alt codes input.
3632+
3633+
inline constexpr char32_t alt_code_oem437[256] = {
3634+
0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
3635+
0x25ba, 0x25c4, 0x2195, 0x203c, 0x00b6, 0x00a7, 0x25ac, 0x21a8, 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc,
3636+
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
3637+
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
3638+
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
3639+
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
3640+
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
3641+
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x2302,
3642+
0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7, 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
3643+
0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9, 0x00ff, 0x00d6, 0x00dc, 0x00a2, 0x00a3, 0x00a5, 0x20a7, 0x0192,
3644+
0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba, 0x00bf, 0x2310, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
3645+
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
3646+
0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f, 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
3647+
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b, 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
3648+
0x03b1, 0x00df, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x00b5, 0x03a4, 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229,
3649+
0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248, 0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0
3650+
};
3651+
3652+
inline constexpr char32_t alt_code_cp1252[256] = {
3653+
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
3654+
0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
3655+
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
3656+
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
3657+
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
3658+
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
3659+
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
3660+
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
3661+
0x20ac, 0x0081, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021, 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008d, 0x017d, 0x008f,
3662+
0x0090, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x009d, 0x017e, 0x0178,
3663+
0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
3664+
0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
3665+
0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
3666+
0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
3667+
0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
3668+
0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
3669+
};

doc/classes/LineEdit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
- To enter edit mode, click on the control with the mouse, see also [member keep_editing_on_text_submit].
1010
- To exit edit mode, press [code]ui_text_submit[/code] or [code]ui_cancel[/code] (by default [kbd]Escape[/kbd]) actions.
1111
- Check [method edit], [method unedit], [method is_editing], and [signal editing_toggled] for more information.
12+
While entering text, it is possible to insert special characters using Unicode, OEM or Windows alt codes:
13+
- To enter Unicode codepoints, hold [kbd]Alt[/kbd] and type the codepoint on the numpad. For example, to enter the character [code]á[/code] (U+00E1), hold [kbd]Alt[/kbd] and type [kbd]+E1[/kbd] on the numpad (the leading zeroes can be omitted).
14+
- To enter OEM codepoints, hold [kbd]Alt[/kbd] and type the code on the numpad. For example, to enter the character [code]á[/code] (OEM 160), hold [kbd]Alt[/kbd] and type [code]160[/code] on the numpad.
15+
- To enter Windows codepoints, hold [kbd]Alt[/kbd] and type the code on the numpad. For example, to enter the character [code]á[/code] (Windows 0225), hold [kbd]Alt[/kbd] and type [kbd]0[/kbd], [kbd]2[/kbd], [kbd]2[/kbd], [kbd]5[/kbd] on the numpad. The leading zero here must [b]not[/b] be omitted, as this is how Windows codepoints are distinguished from OEM codepoints.
1216
[b]Important:[/b]
1317
- Focusing the [LineEdit] with [code]ui_focus_next[/code] (by default [kbd]Tab[/kbd]) or [code]ui_focus_prev[/code] (by default [kbd]Shift + Tab[/kbd]) or [method Control.grab_focus] still enters edit mode (for compatibility).
1418
[LineEdit] features many built-in shortcuts that are always available ([kbd]Ctrl[/kbd] here maps to [kbd]Cmd[/kbd] on macOS):

doc/classes/TextEdit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
</brief_description>
66
<description>
77
A multiline text editor. It also has limited facilities for editing code, such as syntax highlighting support. For more advanced facilities for editing code, see [CodeEdit].
8+
While entering text, it is possible to insert special characters using Unicode, OEM or Windows alt codes:
9+
- To enter Unicode codepoints, hold [kbd]Alt[/kbd] and type the codepoint on the numpad. For example, to enter the character [code]á[/code] (U+00E1), hold [kbd]Alt[/kbd] and type [kbd]+E1[/kbd] on the numpad (the leading zeroes can be omitted).
10+
- To enter OEM codepoints, hold [kbd]Alt[/kbd] and type the code on the numpad. For example, to enter the character [code]á[/code] (OEM 160), hold [kbd]Alt[/kbd] and type [code]160[/code] on the numpad.
11+
- To enter Windows codepoints, hold [kbd]Alt[/kbd] and type the code on the numpad. For example, to enter the character [code]á[/code] (Windows 0225), hold [kbd]Alt[/kbd] and type [kbd]0[/kbd], [kbd]2[/kbd], [kbd]2[/kbd], [kbd]5[/kbd] on the numpad. The leading zero here must [b]not[/b] be omitted, as this is how Windows codepoints are distinguished from OEM codepoints.
812
[b]Note:[/b] Most viewport, caret, and edit methods contain a [code]caret_index[/code] argument for [member caret_multiple] support. The argument should be one of the following: [code]-1[/code] for all carets, [code]0[/code] for the main caret, or greater than [code]0[/code] for secondary carets in the order they were created.
913
[b]Note:[/b] When holding down [kbd]Alt[/kbd], the vertical scroll wheel will scroll 5 times as fast as it would normally do. This also works in the Godot script editor.
1014
</description>

scene/gui/line_edit.cpp

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -671,13 +671,14 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
671671
return;
672672
}
673673

674-
// Start Unicode input (hold).
674+
// Start Unicode Alt input (hold).
675675
if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) {
676676
if (selection.enabled) {
677677
selection_delete();
678678
}
679679
alt_start = true;
680680
alt_code = 0;
681+
alt_mode = ALT_INPUT_UNICODE;
681682
ime_text = "u";
682683
ime_selection = Vector2i(0, -1);
683684
_shape();
@@ -693,6 +694,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
693694
}
694695
alt_start_no_hold = true;
695696
alt_code = 0;
697+
alt_mode = ALT_INPUT_UNICODE;
696698
ime_text = "u";
697699
ime_selection = Vector2i(0, -1);
698700
_shape();
@@ -701,37 +703,101 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
701703
return;
702704
}
703705

706+
// Start OEM Alt input (hold).
707+
if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) {
708+
if (selection.enabled) {
709+
selection_delete();
710+
}
711+
alt_start = true;
712+
alt_code = (uint32_t)(k->get_keycode() - Key::KP_0);
713+
alt_mode = ALT_INPUT_OEM;
714+
ime_text = vformat("o%s", String::num_int64(alt_code, 10));
715+
ime_selection = Vector2i(0, -1);
716+
_shape();
717+
queue_redraw();
718+
accept_event();
719+
return;
720+
}
721+
722+
// Start Windows Alt input (hold).
723+
if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) {
724+
if (selection.enabled) {
725+
selection_delete();
726+
}
727+
alt_start = true;
728+
alt_mode = ALT_INPUT_WIN;
729+
alt_code = 0;
730+
ime_text = "w";
731+
ime_selection = Vector2i(0, -1);
732+
_shape();
733+
queue_redraw();
734+
accept_event();
735+
return;
736+
}
737+
704738
// Update Unicode input.
705739
if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) {
706740
if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
707-
alt_code = alt_code << 4;
741+
if (alt_mode == ALT_INPUT_UNICODE) {
742+
alt_code = alt_code << 4;
743+
} else {
744+
alt_code = alt_code * 10;
745+
}
708746
alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
709747
} else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
710-
alt_code = alt_code << 4;
748+
if (alt_mode == ALT_INPUT_UNICODE) {
749+
alt_code = alt_code << 4;
750+
} else {
751+
alt_code = alt_code * 10;
752+
}
711753
alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
712-
} else if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
754+
} else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
713755
alt_code = alt_code << 4;
714756
alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
715757
} else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) {
716-
alt_code = alt_code << 4;
758+
if (alt_mode == ALT_INPUT_UNICODE) {
759+
alt_code = alt_code << 4;
760+
} else {
761+
alt_code = alt_code * 10;
762+
}
717763
alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0);
718-
} else if ((Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
764+
} else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
719765
alt_code = alt_code << 4;
720766
alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10;
721767
} else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) {
722-
alt_code = alt_code << 4;
768+
if (alt_mode == ALT_INPUT_UNICODE) {
769+
alt_code = alt_code << 4;
770+
} else {
771+
alt_code = alt_code * 10;
772+
}
723773
alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0);
724774
}
725775
if (k->get_keycode() == Key::BACKSPACE) {
726-
alt_code = alt_code >> 4;
776+
if (alt_mode == ALT_INPUT_UNICODE) {
777+
alt_code = alt_code >> 4;
778+
} else {
779+
alt_code = alt_code / 10;
780+
}
727781
}
728782
if (alt_code > 0x10ffff) {
729783
alt_code = 0x10ffff;
730784
}
731785
if (alt_code > 0) {
732-
ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
786+
if (alt_mode == ALT_INPUT_UNICODE) {
787+
ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
788+
} else if (alt_mode == ALT_INPUT_OEM) {
789+
ime_text = vformat("o%s", String::num_int64(alt_code, 10));
790+
} else if (alt_mode == ALT_INPUT_WIN) {
791+
ime_text = vformat("w%s", String::num_int64(alt_code, 10));
792+
}
733793
} else {
734-
ime_text = "u";
794+
if (alt_mode == ALT_INPUT_UNICODE) {
795+
ime_text = "u";
796+
} else if (alt_mode == ALT_INPUT_OEM) {
797+
ime_text = "o";
798+
} else if (alt_mode == ALT_INPUT_WIN) {
799+
ime_text = "w";
800+
}
735801
}
736802
ime_selection = Vector2i(0, -1);
737803
_shape();
@@ -744,11 +810,32 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
744810
if ((!k->is_pressed() && alt_start && k->get_keycode() == Key::ALT) || (alt_start_no_hold && (k->is_action("ui_text_submit", true) || k->is_action("ui_accept", true)))) {
745811
alt_start = false;
746812
alt_start_no_hold = false;
747-
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
813+
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
748814
ime_text = String();
749815
ime_selection = Vector2i();
750-
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
751-
insert_text_at_caret(ucodestr);
816+
if (alt_mode == ALT_INPUT_UNICODE) {
817+
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
818+
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
819+
insert_text_at_caret(ucodestr);
820+
}
821+
} else if (alt_mode == ALT_INPUT_OEM) {
822+
if (alt_code > 0x00 && alt_code <= 0xff) {
823+
char32_t ucodestr[2] = { alt_code_oem437[alt_code], 0 };
824+
insert_text_at_caret(ucodestr);
825+
} else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
826+
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
827+
insert_text_at_caret(ucodestr);
828+
}
829+
} else if (alt_mode == ALT_INPUT_WIN) {
830+
if (alt_code > 0x00 && alt_code <= 0xff) {
831+
char32_t ucodestr[2] = { alt_code_cp1252[alt_code], 0 };
832+
insert_text_at_caret(ucodestr);
833+
} else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
834+
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
835+
insert_text_at_caret(ucodestr);
836+
}
837+
}
838+
alt_mode = ALT_INPUT_NONE;
752839
} else {
753840
ime_text = String();
754841
ime_selection = Vector2i();
@@ -763,6 +850,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
763850
if (alt_start_no_hold && k->is_action("ui_cancel", true)) {
764851
alt_start = false;
765852
alt_start_no_hold = false;
853+
alt_mode = ALT_INPUT_NONE;
766854
ime_text = String();
767855
ime_selection = Vector2i();
768856
_shape();

scene/gui/line_edit.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class LineEdit : public Control {
9292
bool pass = false;
9393
bool text_changed_dirty = false;
9494

95+
enum AltInputMode {
96+
ALT_INPUT_NONE,
97+
ALT_INPUT_UNICODE,
98+
ALT_INPUT_OEM,
99+
ALT_INPUT_WIN,
100+
};
101+
102+
AltInputMode alt_mode = ALT_INPUT_NONE;
95103
bool alt_start = false;
96104
bool alt_start_no_hold = false;
97105
uint32_t alt_code = 0;

0 commit comments

Comments
 (0)