[TextEdit / LineEdit] Add support for OEM Alt codes input.

This commit is contained in:
bruvzg 2024-06-22 15:16:11 +03:00 committed by Pāvels Nadtočajevs
parent c374ce211c
commit aa0ade5b49
No known key found for this signature in database
GPG key ID: 8413210218EF35D2
7 changed files with 257 additions and 25 deletions

View file

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

View file

@ -9,6 +9,10 @@
- To enter edit mode, click on the control with the mouse, see also [member keep_editing_on_text_submit]. - To enter edit mode, click on the control with the mouse, see also [member keep_editing_on_text_submit].
- To exit edit mode, press [code]ui_text_submit[/code] or [code]ui_cancel[/code] (by default [kbd]Escape[/kbd]) actions. - To exit edit mode, press [code]ui_text_submit[/code] or [code]ui_cancel[/code] (by default [kbd]Escape[/kbd]) actions.
- Check [method edit], [method unedit], [method is_editing], and [signal editing_toggled] for more information. - Check [method edit], [method unedit], [method is_editing], and [signal editing_toggled] for more information.
While entering text, it is possible to insert special characters using Unicode, OEM or Windows alt codes:
- 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).
- 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.
- 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.
[b]Important:[/b] [b]Important:[/b]
- 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). - 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).
[LineEdit] features many built-in shortcuts that are always available ([kbd]Ctrl[/kbd] here maps to [kbd]Cmd[/kbd] on macOS): [LineEdit] features many built-in shortcuts that are always available ([kbd]Ctrl[/kbd] here maps to [kbd]Cmd[/kbd] on macOS):

View file

@ -5,6 +5,10 @@
</brief_description> </brief_description>
<description> <description>
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]. 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].
While entering text, it is possible to insert special characters using Unicode, OEM or Windows alt codes:
- 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).
- 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.
- 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.
[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. [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.
[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. [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.
</description> </description>

View file

@ -671,13 +671,14 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
return; return;
} }
// Start Unicode input (hold). // Start Unicode Alt input (hold).
if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) { if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) {
if (selection.enabled) { if (selection.enabled) {
selection_delete(); selection_delete();
} }
alt_start = true; alt_start = true;
alt_code = 0; alt_code = 0;
alt_mode = ALT_INPUT_UNICODE;
ime_text = "u"; ime_text = "u";
ime_selection = Vector2i(0, -1); ime_selection = Vector2i(0, -1);
_shape(); _shape();
@ -693,6 +694,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
} }
alt_start_no_hold = true; alt_start_no_hold = true;
alt_code = 0; alt_code = 0;
alt_mode = ALT_INPUT_UNICODE;
ime_text = "u"; ime_text = "u";
ime_selection = Vector2i(0, -1); ime_selection = Vector2i(0, -1);
_shape(); _shape();
@ -701,37 +703,101 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
return; return;
} }
// Start OEM Alt input (hold).
if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) {
if (selection.enabled) {
selection_delete();
}
alt_start = true;
alt_code = (uint32_t)(k->get_keycode() - Key::KP_0);
alt_mode = ALT_INPUT_OEM;
ime_text = vformat("o%s", String::num_int64(alt_code, 10));
ime_selection = Vector2i(0, -1);
_shape();
queue_redraw();
accept_event();
return;
}
// Start Windows Alt input (hold).
if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) {
if (selection.enabled) {
selection_delete();
}
alt_start = true;
alt_mode = ALT_INPUT_WIN;
alt_code = 0;
ime_text = "w";
ime_selection = Vector2i(0, -1);
_shape();
queue_redraw();
accept_event();
return;
}
// Update Unicode input. // Update Unicode input.
if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) { if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) {
if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) { if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0); alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
} else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) { } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)(k->get_keycode() - Key::KP_0); alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
} else if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) { } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
alt_code = alt_code << 4; alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10; alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
} else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) { } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0); alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0);
} else if ((Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) { } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
alt_code = alt_code << 4; alt_code = alt_code << 4;
alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10; alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10;
} else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) { } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0); alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0);
} }
if (k->get_keycode() == Key::BACKSPACE) { if (k->get_keycode() == Key::BACKSPACE) {
alt_code = alt_code >> 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code >> 4;
} else {
alt_code = alt_code / 10;
}
} }
if (alt_code > 0x10ffff) { if (alt_code > 0x10ffff) {
alt_code = 0x10ffff; alt_code = 0x10ffff;
} }
if (alt_code > 0) { if (alt_code > 0) {
ime_text = vformat("u%s", String::num_int64(alt_code, 16, true)); if (alt_mode == ALT_INPUT_UNICODE) {
ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
} else if (alt_mode == ALT_INPUT_OEM) {
ime_text = vformat("o%s", String::num_int64(alt_code, 10));
} else if (alt_mode == ALT_INPUT_WIN) {
ime_text = vformat("w%s", String::num_int64(alt_code, 10));
}
} else { } else {
ime_text = "u"; if (alt_mode == ALT_INPUT_UNICODE) {
ime_text = "u";
} else if (alt_mode == ALT_INPUT_OEM) {
ime_text = "o";
} else if (alt_mode == ALT_INPUT_WIN) {
ime_text = "w";
}
} }
ime_selection = Vector2i(0, -1); ime_selection = Vector2i(0, -1);
_shape(); _shape();
@ -744,11 +810,32 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
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)))) { 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)))) {
alt_start = false; alt_start = false;
alt_start_no_hold = false; alt_start_no_hold = false;
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) { if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
ime_text = String(); ime_text = String();
ime_selection = Vector2i(); ime_selection = Vector2i();
char32_t ucodestr[2] = { (char32_t)alt_code, 0 }; if (alt_mode == ALT_INPUT_UNICODE) {
insert_text_at_caret(ucodestr); if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
insert_text_at_caret(ucodestr);
}
} else if (alt_mode == ALT_INPUT_OEM) {
if (alt_code > 0x00 && alt_code <= 0xff) {
char32_t ucodestr[2] = { alt_code_oem437[alt_code], 0 };
insert_text_at_caret(ucodestr);
} else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
insert_text_at_caret(ucodestr);
}
} else if (alt_mode == ALT_INPUT_WIN) {
if (alt_code > 0x00 && alt_code <= 0xff) {
char32_t ucodestr[2] = { alt_code_cp1252[alt_code], 0 };
insert_text_at_caret(ucodestr);
} else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
insert_text_at_caret(ucodestr);
}
}
alt_mode = ALT_INPUT_NONE;
} else { } else {
ime_text = String(); ime_text = String();
ime_selection = Vector2i(); ime_selection = Vector2i();
@ -763,6 +850,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
if (alt_start_no_hold && k->is_action("ui_cancel", true)) { if (alt_start_no_hold && k->is_action("ui_cancel", true)) {
alt_start = false; alt_start = false;
alt_start_no_hold = false; alt_start_no_hold = false;
alt_mode = ALT_INPUT_NONE;
ime_text = String(); ime_text = String();
ime_selection = Vector2i(); ime_selection = Vector2i();
_shape(); _shape();

View file

@ -92,6 +92,14 @@ private:
bool pass = false; bool pass = false;
bool text_changed_dirty = false; bool text_changed_dirty = false;
enum AltInputMode {
ALT_INPUT_NONE,
ALT_INPUT_UNICODE,
ALT_INPUT_OEM,
ALT_INPUT_WIN,
};
AltInputMode alt_mode = ALT_INPUT_NONE;
bool alt_start = false; bool alt_start = false;
bool alt_start_no_hold = false; bool alt_start_no_hold = false;
uint32_t alt_code = 0; uint32_t alt_code = 0;

View file

@ -1947,13 +1947,14 @@ void TextEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) { bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
Ref<InputEventKey> k = p_gui_input; Ref<InputEventKey> k = p_gui_input;
if (k.is_valid()) { if (k.is_valid()) {
// Start Unicode input (hold). // Start Unicode Alt input (hold).
if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) { if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) {
if (has_selection()) { if (has_selection()) {
delete_selection(); delete_selection();
} }
alt_start = true; alt_start = true;
alt_code = 0; alt_code = 0;
alt_mode = ALT_INPUT_UNICODE;
ime_text = "u"; ime_text = "u";
ime_selection = Vector2i(0, -1); ime_selection = Vector2i(0, -1);
_update_ime_text(); _update_ime_text();
@ -1967,43 +1968,104 @@ bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
} }
alt_start_no_hold = true; alt_start_no_hold = true;
alt_code = 0; alt_code = 0;
alt_mode = ALT_INPUT_UNICODE;
ime_text = "u"; ime_text = "u";
ime_selection = Vector2i(0, -1); ime_selection = Vector2i(0, -1);
_update_ime_text(); _update_ime_text();
return true; return true;
} }
// Start OEM Alt input (hold).
if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) {
if (has_selection()) {
delete_selection();
}
alt_start = true;
alt_code = (uint32_t)(k->get_keycode() - Key::KP_0);
alt_mode = ALT_INPUT_OEM;
ime_text = vformat("o%s", String::num_int64(alt_code, 10));
ime_selection = Vector2i(0, -1);
_update_ime_text();
return true;
}
// Start Windows Alt input (hold).
if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) {
if (has_selection()) {
delete_selection();
}
alt_start = true;
alt_mode = ALT_INPUT_WIN;
alt_code = 0;
ime_text = "w";
ime_selection = Vector2i(0, -1);
_update_ime_text();
return true;
}
// Update Unicode input. // Update Unicode input.
if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) { if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) {
if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) { if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0); alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
} else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) { } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)(k->get_keycode() - Key::KP_0); alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
} else if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) { } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
alt_code = alt_code << 4; alt_code = alt_code << 4;
alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10; alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
} else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) { } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0); alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0);
} else if ((Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) { } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
alt_code = alt_code << 4; alt_code = alt_code << 4;
alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10; alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10;
} else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) { } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) {
alt_code = alt_code << 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code << 4;
} else {
alt_code = alt_code * 10;
}
alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0); alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0);
} }
if (k->get_keycode() == Key::BACKSPACE) { if (k->get_keycode() == Key::BACKSPACE) {
alt_code = alt_code >> 4; if (alt_mode == ALT_INPUT_UNICODE) {
alt_code = alt_code >> 4;
} else {
alt_code = alt_code / 10;
}
} }
if (alt_code > 0x10ffff) { if (alt_code > 0x10ffff) {
alt_code = 0x10ffff; alt_code = 0x10ffff;
} }
if (alt_code > 0) { if (alt_code > 0) {
ime_text = vformat("u%s", String::num_int64(alt_code, 16, true)); if (alt_mode == ALT_INPUT_UNICODE) {
ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
} else if (alt_mode == ALT_INPUT_OEM) {
ime_text = vformat("o%s", String::num_int64(alt_code, 10));
} else if (alt_mode == ALT_INPUT_WIN) {
ime_text = vformat("w%s", String::num_int64(alt_code, 10));
}
} else { } else {
ime_text = "u"; if (alt_mode == ALT_INPUT_UNICODE) {
ime_text = "u";
} else if (alt_mode == ALT_INPUT_OEM) {
ime_text = "o";
} else if (alt_mode == ALT_INPUT_WIN) {
ime_text = "w";
}
} }
ime_selection = Vector2i(0, -1); ime_selection = Vector2i(0, -1);
_update_ime_text(); _update_ime_text();
@ -2014,10 +2076,27 @@ bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
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)))) { 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)))) {
alt_start = false; alt_start = false;
alt_start_no_hold = false; alt_start_no_hold = false;
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) { if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
ime_text = String(); ime_text = String();
ime_selection = Vector2i(); ime_selection = Vector2i();
handle_unicode_input(alt_code); if (alt_mode == ALT_INPUT_UNICODE) {
if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
handle_unicode_input(alt_code);
}
} else if (alt_mode == ALT_INPUT_OEM) {
if (alt_code > 0x00 && alt_code <= 0xff) {
handle_unicode_input(alt_code_oem437[alt_code]);
} else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
handle_unicode_input(alt_code);
}
} else if (alt_mode == ALT_INPUT_WIN) {
if (alt_code > 0x00 && alt_code <= 0xff) {
handle_unicode_input(alt_code_cp1252[alt_code]);
} else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
handle_unicode_input(alt_code);
}
}
alt_mode = ALT_INPUT_NONE;
} else { } else {
ime_text = String(); ime_text = String();
ime_selection = Vector2i(); ime_selection = Vector2i();
@ -2030,6 +2109,7 @@ bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) {
if (alt_start_no_hold && k->is_action("ui_cancel", true)) { if (alt_start_no_hold && k->is_action("ui_cancel", true)) {
alt_start = false; alt_start = false;
alt_start_no_hold = false; alt_start_no_hold = false;
alt_mode = ALT_INPUT_NONE;
ime_text = String(); ime_text = String();
ime_selection = Vector2i(); ime_selection = Vector2i();
_update_ime_text(); _update_ime_text();

View file

@ -288,6 +288,14 @@ private:
Text text; Text text;
bool setting_text = false; bool setting_text = false;
enum AltInputMode {
ALT_INPUT_NONE,
ALT_INPUT_UNICODE,
ALT_INPUT_OEM,
ALT_INPUT_WIN,
};
AltInputMode alt_mode = ALT_INPUT_NONE;
bool alt_start = false; bool alt_start = false;
bool alt_start_no_hold = false; bool alt_start_no_hold = false;
uint32_t alt_code = 0; uint32_t alt_code = 0;