Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: IME mode after pressing Escape or Enter key in CJK input #1304

Closed
Tracked by #6999
qqkookie opened this issue Jun 18, 2019 · 21 comments · Fixed by #17067
Closed
Tracked by #6999

Feature Request: IME mode after pressing Escape or Enter key in CJK input #1304

qqkookie opened this issue Jun 18, 2019 · 21 comments · Fixed by #17067
Assignees
Labels
Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) In-PR This issue has a related PR Issue-Task It's a feature request, but it doesn't really need a major design. Needs-Tag-Fix Doesn't match tag requirements Priority-2 A description (P2) Product-Terminal The new Windows Terminal.
Milestone

Comments

@qqkookie
Copy link

When CJK input method (IME) is used, pressing Escape key or Enter key will restore IME mode to ALPHA input mode. Keeping CJK mode in command line input after these keys causes mostly erroneous input. This behavior may be per-user configurable option.

@qqkookie qqkookie added the Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. label Jun 18, 2019
@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jun 18, 2019
@zadjii-msft
Copy link
Member

@qqkookie is this for the Windows Terminal, or conhost.exe (which is the window for cmd.exe, powershell, wsl.exe, etc.)?

@zadjii-msft zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Jun 18, 2019
@DHowett-MSFT DHowett-MSFT removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Jun 18, 2019
@qqkookie
Copy link
Author

qqkookie commented Jun 19, 2019

For Window Terminal. I have implemented similar feature for Cygwin / mintty. You can see my implementation in qqkookie/mintty@c7fb70e

Many CJK terminal emulators (SSH/Telnet clients) on MS Windows have similar feature. For example, Korean iPuTTY (open source), XShell (commercial SSH client) and many X window Korean/Hanguel terminal emulator like hanterm. It is such universally requested feature for Korean terminal users.

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Jun 19, 2019
@DHowett-MSFT DHowett-MSFT added Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal. and removed Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. labels Nov 4, 2019
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Nov 4, 2019
@DHowett-MSFT DHowett-MSFT removed the Needs-Attention The core contributors need to come back around and look at this ASAP. label Nov 4, 2019
@DHowett-MSFT DHowett-MSFT added this to the Terminal v1.0 milestone Nov 4, 2019
@qqkookie
Copy link
Author

qqkookie commented Nov 12, 2019

I examined current source myself. It seems that CJK IME is handled by console host. ConHost/Host/{ input.c and conimeinfo.c_. I guess that the HandleKeyEvent() routine in $(ROOT)/src/interactivity/win32/windowio.cpp is suitable place to handle this feature.

Insert code like below in the HandleKeyEvent() function.

if ((key == VK_RETURN || key == VK_ESCAPE) && !mods) {
    HIMC hImc = ImmGetContext(wnd);
    if (ImmGetOpenStatus(hImc)) {
      ImmSetConversionStatus(hImc, IME_CMODE_ALPHANUMERIC, IME_SMODE_NONE);
      ImmReleaseContext(wnd, hImc);
    };
  };

@zadjii-msft
Copy link
Member

@qqkookie great work finding that in the code. However, what you've found there is the code that is responsible for conhost.exe's IME handling. conhost.exe is the vintage console window, and it's not really accepting new feature PRs like that.

Unfortunately, with #2213, the IME in the Windows Terminal doesn't currently work. There's a PR in progress for it in #1919. Once that's in, then it might be more appropriate to investigate this feature for the Windows Terminal 😊

@DHowett-MSFT
Copy link
Contributor

From @eromoe:

@DHowett-MSFT How is going on ? This problem still exists in 0.11 . We usually open a terminal type some command then close it , this problem is very annoying .

Let me express again:

  1. Most people type english in terminal
  2. Terminal automatically enable input method(not prime, it is second one) at startup , no other softwares do this .
  3. Terminal automatically enable a broken input method , which behaviour is different from the original input metod, mainly lost words select box. Need quit input method , enter again to gain words select box , see below gif :

tm

@DHowett-MSFT
Copy link
Contributor

I've looked high and low at CoreTextEditContext, and I just can't find the equivalent API for ImmSetConversionStatus. Have to ask our input folks.

@DHowett DHowett assigned DHowett and unassigned DHowett-MSFT May 17, 2020
@cinnamon-msft cinnamon-msft removed this from the Terminal v1.1 milestone Jun 8, 2020
@eromoe
Copy link

eromoe commented Oct 9, 2020

It not limit to particular IME . The key problem is :

  • windows 10 would not create a default English IME as XP / win7 did .

This make other IME shows on input area at default . Usually I don't expect that , I manully add an English IME to overcome currently .

This happened on terminal is very annoy .

@skyline75489
Copy link
Collaborator

@eromoe By saying "creating a default English IME" do you mean creating one after deleting the default Microsoft Pinyin? I think that people inside the Windows OS team may have more knowledge about this.

Personally I'm happy with the performance and reliability of the stock Microsoft Pinyin, especially after 2004 release. Overall the IME experience does not cause me serious problems.

@eromoe
Copy link

eromoe commented Oct 9, 2020

@skyline75489
Yes , Microsoft Pinyin is at same position as sougou Pinyin . We only need plain English input(in English Language) as default without selection box : image

Below setting doesn't work with English input(in Chinese Language) :
image

I use ctrl+shift to switch when I need to type native word , this is a habit since XP . I feel very bad when everytime focused on a input box along with native IME word selection box show up , because we usually need type alphabet and number .

@skyline75489
Copy link
Collaborator

Ah. I see. So the issue for Sogou can be separated into two parts:

  1. When users uninstall the stock Microsoft Pinyin (which is the default IME in a zh-CN version of Win10) and install third party IME like sogou, which make itself the default IME, there will only be one IME in the OS and will be activated when every program launches. This is fine for most programs but not for WT, which usually expects ASCII things.
  2. For some reason, Sogou is not behaving correctly in WT as in other programs, making it even more painful for people to use. In my experience, this actually happens a lot with third party IMEs. Those IMEs like Sogou was born since the age of Win98/XP and keeps evolving since then. Microsoft Pinyin was terrible in those days so third party IMEs gained a huge amount of market share. But third party IMEs may not work well with applications that does not even expect third party IMEs. I think this should fall into the same category as Make sure we don't hide the cursor until the IME starts #7673 which itself is an internal bug report from an IME vendor.

As for the IME configuration, personally I have this:

image

I'm using the en-US version of Windows so naturally I have en-US at first, then I installed the zh-CH language pack and got the Microsoft Pinyin. I guess this is why I'm not experience the IME difficulty.

@eromoe
Copy link

eromoe commented Oct 9, 2020

Above is a little diverge , previous Key Problem I said is mainly point to that comment in Chinese. It is not limit to Sougou .

The key points of this issue I had expressed by the gif, these are definitely bugs :

  1. Terminal automatically enable input method(not prime, it is second one) at startup , no other softwares do this .

    It doesn't recognize English input(in Chinese Language) at top(prime/default position) , but would use English input(in English Language) when it is on top

  2. Terminal automatically enable a broken input method , which behaviour is different from the original input metod, mainly lost words select box. Need quit input method , enter again to gain words select box , see below gif :

    even the Chinese IME is broken , not limit to Sougou/Microsoft IME as I remenber .

tm

@Refactoring
Copy link

As 'skyline75489' and 'eromoe' say, windows 10 would not create a default English IME as XP / win7 did。But, Sogou Pinyin set the wrong English input , which led to the problem in Gif. The correct way to add gif below me is explained.

ReproduceAndResolves_Small

@YanceyChiew
Copy link
Contributor

I believe that changing this line (/src/cascadia/TerminalControl/TSFInputControl.cpp#L41)

        _editContext.InputScope(CoreTextInputScope::Text);

to

        _editContext.InputScope(CoreTextInputScope::AlphanumericHalfWidth);

should make CJK IME open in English input mode by default.

But the problem is that after switching to composition mode(eg, Chinese Pinyin), it will be difficult to switch back dynamically through code. In fact I found a workaround but it raises other problems:

                // c# sample code    
                case VirtualKey.Enter:
                    _editContext.InputScope = CoreTextInputScope.Password;
                    _editContext.NotifyFocusLeave();
                    _editContext.NotifyFocusEnter();
                    _editContext.InputScope = CoreTextInputScope.AlphanumericHalfWidth;
                    break;

When inputting through the physical keyboard, the next time you switch to composition mode you will need to press shift twice (normally only once).

A more serious problem occurs when using the soft keyboard for input. If speculate from the code it may cause the soft keyboard to flash once or twice, which already affects the user experience, but the reality is worse, soft keyboard will be fixed to the password mode and cannot be switched.

The soft keyboard behaves very strangely, If you adjust where _editContext.NotifyFocusLeave() and _editContext.NotifyFocusEnter() appear in the code, the input mode of the soft keyboard can also be switched to English, but another same(Space, Enter or Escape, other key bindings seem do not work) keystroke will make it switch back. Physical keyboard keystrokes are invalid at this time, although the input method indicator in the status bar shows its desired state.

                // c# sample code
                case VirtualKey.Space:


                    _editContext.NotifyFocusLeave();
                    _editContext.InputScope = CoreTextInputScope.Password;  
                    _editContext.NotifyFocusEnter();

                    _editContext.NotifyFocusLeave();
                    _editContext.InputScope = CoreTextInputScope.AlphanumericHalfWidth;
                    _editContext.NotifyFocusEnter();



                    break;

I didn't find an API that really fits, maybe it just doesn't exist. For me it was enough to have windows terminal support English input by default when it was opened

@qqkookie
Copy link
Author

qqkookie commented Jun 18, 2023

Problem of CJK IME on terminal start up is related but different matter, I guess.
Most CJK IME is in alpha mode on first activation. Most CJK IME's are capable of typing English alphabet or US-ASCII input without switching to US-ASCII IME. We just have to tell CJK IME to change mode from CJK composition mode to ALPHA mode, not switching from CJK IME to US ASCII IME. Currently this mode change is done manually by pressing <Right-Alt> key on standard US IBM 101 type keyboard or dedicated Alpha/CJK conversion key on CJK-localized IBM 103 type keyboard.

So we should call ImmSetConversionStatus(hImc, IME_CMODE_ALPHANUMERIC, IME_SMODE_NONE); on <Escape or <Return> key input.

I tried to insert such code into src/host/input.cpp, HandleGenericKeyEvent(), but failed.
The host/input.c is not right place to call IME call.

I also tried src/interactivity/win32/Windowio.cpp, HandleKeyEvent(), and
src/terminal/input/terminalinput.cpp TerminalInput::HandleKey(), but both failed, too.

src/terminal/input/terminalinput.cpp TerminalInput::HandleKey() seems to be very promising.
But I can't get console window handle & IME context with GetConsoleWindow() & ImmGetContext()

Any help or idea?

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Tag-Fix Doesn't match tag requirements label Jun 20, 2023
@qqkookie qqkookie reopened this Jun 20, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Tag-Fix Doesn't match tag requirements label Jun 20, 2023
@qqkookie
Copy link
Author

I made good progress. Now it works on my PC, at least. I need more testing, I guess.
Place to add the IMM calls above is "src/terminal/input/terminalInput.cpp" : TerminalInput::HandleKey()
Now I need to add configuration option to turn this behavior on/off.
Any idead or help?

@qqkookie
Copy link
Author

qqkookie commented Jun 22, 2023

Hmm.. I added terminal Setting/Interaction UI and it works.
But I cannot apply the global terminal setting to TerminalInput::HandleKey(). The global boolean variable which controls this feature on/off is _setting.KeyAlphaMode or ServiceLocator::LocateGlobals().KeyAlphaMode(). But both are inaccessible or unusable in the TerminalInput class. I am not familiar with overall structure of this project. I guess that the global _settings are defined and used in few places: IControlSettings, InteractionViewModel, TerminalSettings.h, GlobalAppSettings and others. But none of them is usable in src/terminal/input/terminalInput.cpp.
How to add new feature control setting/option to shared library function? Anyone of such experience or knowledge?
Here is my work in progress: @qqkookie/terminal/tree/Kookie

@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR This issue has a related PR label Jun 24, 2023
github-merge-queue bot pushed a commit that referenced this issue Apr 18, 2024
Next in the popular series of minor refactorings:
Out with the old, in with the new!

This PR removes all of the existing TSF code, both for conhost and
Windows Terminal. conhost's TSF implementation was awful:
It allocated an entire text buffer _per line_ of input.
Additionally, its implementation spanned a whopping 40 files and
almost 5000 lines of code. Windows Terminal's implementation was
absolutely fine in comparison, but it was user unfriendly due to
two reasons: Its usage of the `CoreTextServices` WinRT API indirectly
meant that it used a non-transitory TSF document, which is not the
right choice for a terminal. A `TF_SS_TRANSITORY` document (-context)
indicates to TSF that it cannot undo a previously completed composition
which is exactly what we need: Once composition has completed we send
the result to the shell and we cannot undo this later on.
The WinRT API does not allow us to use `TF_SS_TRANSITORY` and so it's
unsuitable for our application. Additionally, the implementation used
XAML to render the composition instead of being part of our text
renderer, which resulted in the text looking weird and hard to read.

The new implementation spans just 8 files and is ~1000 lines which
should make it significantly easier to maintain. The architecture is
not particularly great, but it's certainly better than what we had.
The implementation is almost entirely identical between both conhost
and Windows Terminal and thus they both also behave identical.
It fixes an uncountable number of subtle bugs in the conhost TSF
implementation, as it failed to check for status codes after calls.
It also adds several new features, like support for wavy underlines
(as used by the Japanese IME), dashed underlines (the default for
various languages now, like Vietnamese), colored underlines,
colored foreground/background controlled by the IME, and more!

I have tried to replicate the following issues and have a high
confidence that they're resolved now:
Closes #1304
Closes #3730
Closes #4052
Closes #5007  (as it is not applicable anymore)
Closes #5110
Closes #6186
Closes #6192
Closes #13805
Closes #14349
Closes #14407
Closes #16180

For the following issues I'm not entirely sure if it'll fix it,
but I suspect it's somewhat likely:
#13681
#16305
#16817

Lastly, there's one remaining bug that I don't know how to resolve.
However, that issue also plagues conhost and Windows Terminal
right now, so it's at least not a regression:
* Press Win+. (emoji picker) and close it
* Move the window around
* Press Win+.

This will open the emoji picker at the old window location.
It also occurs when the cursor moves within the window.
While this is super annoying, I could not find a way to fix it.

## Validation Steps Performed
* See the above closed issues
* Use Vietnamese Telex and type "xin choaf"
  Results in "xin chào" ✅
* Use the MS Japanese IME and press Alt+`
  Toggles between the last 2 modes ✅
* Use the MS Japanese IME, type "kyouhaishaheiku", and press Space
  * The text is converted, underlined and the first part is
    doubly underlined ✅
  * Left/Right moves between the 3 segments ✅
  * Home/End moves between start/end ✅
  * Esc puts a wavy line under the current segment ✅
* Use the Korean IME, type "gksgks"
  This results in "한한" ✅
* Use the Korean IME, type "gks", and press Right Ctrl
  Opens a popup which allows you to navigate with Arrow/Tab keys ✅
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Tag-Fix Doesn't match tag requirements label Apr 18, 2024
@lhecker
Copy link
Member

lhecker commented Apr 18, 2024

We just published a major update to our IME implementation in the nightly Canary branch. It was rewritten from the ground up and has tons of improvements! If you're interested in trying it out, you can get it here: https://aka.ms/terminal-canary-installer
If you already have the Canary build installed, you can use this link to force an update.

If you encounter any issues or have any suggestions, or if you simply like/dislike the changes, please let us know! Thank you for bearing with us. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) In-PR This issue has a related PR Issue-Task It's a feature request, but it doesn't really need a major design. Needs-Tag-Fix Doesn't match tag requirements Priority-2 A description (P2) Product-Terminal The new Windows Terminal.
Projects
None yet