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

cursor show error when use <shift> to switch english input from chinese input (pinyin) #4541

Closed
ackerr opened this issue Jan 19, 2022 · 51 comments
Labels

Comments

@ackerr
Copy link

ackerr commented Jan 19, 2022

Describe the bug
I often use shift to cancel the pre edit with chinese intput method.
In kitty, the cursor block will show error, but in iTerm2, it will use the pre edit word.
ps: i use the sogou app

Screenshots
In kitty

2022-01-19.2.20.53.mov

In iTerm2

2022-01-19.2.24.51.mov

Environment details

kitty 0.24.1 created by Kovid Goyal
Darwin ackerr 20.4.0 Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:47 PDT 2021; root:xnu-7195.101.2~1/RELEASE_X86_64 x86_64
ProductName:	macOS ProductVersion:	11.3.1 BuildVersion:	20E241
Frozen: True
Paths:
  kitty: /Applications/kitty.app/Contents/MacOS/kitty
  base dir: /Applications/kitty.app/Contents/Resources/kitty
  extensions dir: /Applications/kitty.app/Contents/Resources/Python/lib/kitty-extensions
  system shell: /bin/zsh

Config options different from defaults:

Additional context
The same problem when i use kitty --config NONE

@ackerr ackerr added the bug label Jan 19, 2022
@ackerr ackerr changed the title cursor show error when use <shift> to cancel pre chinese input cursor show error when use <shift> to switch english input from chinese input Jan 19, 2022
@ackerr ackerr changed the title cursor show error when use <shift> to switch english input from chinese input cursor show error when use <shift> to switch english input from chinese input (pinyin) Jan 19, 2022
@page-down
Copy link
Contributor

This one has been on my list for a while.

I can reproduce this issue in other input methods.
Maybe need to find some time to try to solve it.

@ackerr
Copy link
Author

ackerr commented Jan 19, 2022

Thank you for reply.

@kovidgoyal
Copy link
Owner

@page-down Shouldn't this be already handled in keyDown where it tests if the NSTextInputContext has been changed?

@page-down
Copy link
Contributor

Shouldn't this be already handled in keyDown ...

NSTextInputContext? Not sure if I follow...

No, the input source was not changed, this state transition occurred in the IME itself. It looks like the input method initiated the text cancellation.

@kovidgoyal
Copy link
Owner

Ah then you just need to see which selector is called for that, and
implement cancellation there.

@page-down
Copy link
Contributor

  • Executed to insertText
  • The text is not inserted.
  • After manually adding the following patch, the pre edit text is cleared.

glfw/cocoa_window.m

- (void)insertText: ...
{
    // ...
    if ([self hasMarkedText]) {
        [self unmarkText];
        marked_text_cleared_by_insert = true;
        // <---- HERE
        GLFWkeyevent dummy = {.action = GLFW_RELEASE, .ime_state = GLFW_IME_PREEDIT_CHANGED};
        _glfwInputKeyboard(window, &dummy);
    }

However, it seems that there is no chance to reach the following location. The shift key does not reach keydown.

- (void)keyDown:(NSEvent *)event
{
// ...
    bool bracketed_ime = false;
    if (!window->ns.deadKeyState) {
        if ([self hasMarkedText]) {
            if (!marked_text_cleared_by_insert) {
                UPDATE_PRE_EDIT_TEXT;
            } else bracketed_ime = true; // <----- any chances?
// ...
    if (bracketed_ime) {
        // insertText followed by setMarkedText
        CLEAR_PRE_EDIT_TEXT; // <----- any chances?
    }
on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000

	insertText: n replacementRange: (9223372036854775807, 0)
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: '' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
// <---- PATCH HERE dummy release
on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000

@kovidgoyal
Copy link
Owner

Why is it not reaching that line? Where is it returning? line 1184?

@page-down
Copy link
Contributor

The modifier keys doesn't reach the keyDown, right?

There will be no return because it is not entered. The insertText is executed directly.

@kovidgoyal
Copy link
Owner

Ah right its a modifier key, so have a check for this inserted in flagsChanged. If marked text goes from existing to cleared send a clear preedit event.

@kovidgoyal
Copy link
Owner

Or is inserttext called before or after flagschanged? Yeah it must be since we dont call interpretkeyevents in flagschanged. So I guess this is unfixable unless we can figure out how cocoa calls flagschanged and check markedtext before it is called.

@page-down
Copy link
Contributor

flagsChanged and then insertText.

If the text cannot be inserted, at least the pre edit text can be cleaned up.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 19, 2022 via email

@page-down
Copy link
Contributor

I tried it and this clears the preedit text properly, need to deal with modifier keys like num_lock.
(After pressing num_lock, it should not be cleared, because it is still in the input method state.)

But for now, it can only be cleared, and maybe after that we'll see if we can insert text. Since I know some input methods use modifier keys to select candidates.

Also found two issues that I haven't had time to go through in detail.

Enter the text, press Tab, and the input is cancelled, but the preedit text is still there.

---------------- key down -------------------
NSEvent: type=KeyDown loc=(897,694.016) time=34154.7 flags=0x100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Press: native_key: 0x28 (k) glfw_key: 0x6b mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: k selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: k marked_text: (k)
on_key_input: glfw key: 0x6b native_code: 0x28 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=290.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(897,694.016) time=34154.8 flags=0x100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Release: native_key: 0x28 (k) glfw_key: 0x6b mods: none 
on_key_input: glfw key: 0x6b native_code: 0x28 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
---------------- key down -------------------
NSEvent: type=KeyDown loc=(897,694.016) time=34155.9 flags=0x100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="	" unmodchars="	" repeat=0 keyCode=48
Press: native_key: 0x30 (<cc>) glfw_key: 0xe002 mods: none char_count: 1 deadKeyState: 0 repeat: 0 text: <none> glfw_key: TAB marked_text: (k)
on_key_input: glfw key: 0xe002 native_code: 0x30 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(897,694.016) time=34156.0 flags=0x100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="	" unmodchars="	" repeat=0 keyCode=48
Release: native_key: 0x30 (<cc>) glfw_key: 0xe002 mods: none 

Enter the text and press F1, the preedit text is cleared, however, it is still in the input method state.

---------------- key down -------------------
NSEvent: type=KeyDown loc=(919,62.0156) time=34638.8 flags=0x100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Press: native_key: 0x28 (k) glfw_key: 0x6b mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: k selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: k marked_text: (k)
on_key_input: glfw key: 0x6b native_code: 0x28 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=290.000000, top=51.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(919,62.0156) time=34638.9 flags=0x100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Release: native_key: 0x28 (k) glfw_key: 0x6b mods: none 
on_key_input: glfw key: 0x6b native_code: 0x28 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=51.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(919,62.0156) time=34640.8 flags=0x800100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="" unmodchars="" repeat=0 keyCode=122
Press: native_key: 0x7a (<cc>) glfw_key: 0xe014 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText:  replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: F1 marked_text: ()
on_key_input: glfw key: 0xe014 native_code: 0x7a action: PRESS mods: none text: '' state: 1 updateIMEPosition: left=298.000000, top=51.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(919,62.0156) time=34640.8 flags=0x800100 win=0x7fa88f3a0680 winNum=2143 ctxt=0x0 chars="" unmodchars="" repeat=0 keyCode=122
Release: native_key: 0x7a (<cc>) glfw_key: 0xe014 mods: none 

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 19, 2022

these are both the same issue, look at line 1184. Basically in order to
handle these we need to know when the IME is canceled, I dont see any
API in cocoa to do that.

@kovidgoyal
Copy link
Owner

One possibility is to override activate/deactivate in GLFWTextInputContext and keep track of whether it is active or not with that.

@page-down
Copy link
Contributor

Basically in order to handle these we need to know when the IME is canceled ...

I'm not sure if this concept is present in macOS's input method framework, perhaps another way of thinking is needed.

One possibility is to override activate/deactivate in GLFWTextInputContext ...

I tested both of them and they are basically independent of the input method state (US/ABC is also an input source, so is it equivalent to being active all the time?) The activation state changes with the window focus.

In fact, insertText is also triggered when the window loses focus, so the problem that needs to be solved at this point is no longer related to the key event.

I tested the behavior of iTerm2, after entering kitt and switching OS window focus, kitt will be entered into the child program.

@page-down
Copy link
Contributor

I did the test again and found that different input methods behave differently. The way we currently handle it (for windows losing focus) is something that needs to be improved.

There are input methods that will output the unconfirmed text in other programs (iTerm2) immediately after losing focus. For some input methods, the pre edit state is maintained after switching focus, and you can continue typing after switching back.

However, in kitty, the current way of handling it is to just clean up the pre edit text, causing the overlay text to disappear when the focus comes back.

From a personal point of view, I am more care about the accurate response to keyboard events, while the input method just needs to be usable under normal circumstances (e.g., back to the topic, being able to press shift and response).

@kovidgoyal
Copy link
Owner

I really dont care about handling focus change. As for keys that dont produce text, if there is no way to query the state of the IME system then I dont see how we can handle them. I suppose one could track the selector the ime system calls and if it doesnt call any, then clear pre-edit. This will work if backspace/delete call a selector.

@page-down
Copy link
Contributor

page-down commented Jan 20, 2022

I tried the latest changes and still have the problem.

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' ...
        insertText: k replacementRange: (...)
on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' ...
if (!in_key_down && !_glfw.ns.text[0]) 

In this case, _glfw.ns.text has text content.

EDIT:

aka that flag is not set, and the text is removed, send a
clear pre-edit message to kitty

I tried it and this clears the preedit text properly ...

Sorry, I tested it before with ![self hasMarkedText] <-- This condition always true, because [self unmarkText] has already been sent.

@page-down
Copy link
Contributor

The logic of keyDown is to wait for insertText to be called after interpretKeyEvents is called, and then send the text via glfw keyevent.

// keyDown:
    glfw_keyevent.text = _glfw.ns.text;
    glfw_keyevent.ime_state = GLFW_IME_NONE;
    add_alternate_keys(&glfw_keyevent, event);
    _glfwInputKeyboard(window, &glfw_keyevent);

However, in this case, the last part of the keyDown code will not be executed after insertText, so can it be sent directly?

@kovidgoyal
Copy link
Owner

1f4372b

@page-down
Copy link
Contributor

1f4372b

Hmm... I tried the above commit.
The problem is still the same.

strlen(utf8) == 1

@kovidgoyal
Copy link
Owner

What is the contents of utf8?

@kovidgoyal
Copy link
Owner

From your previous post it looks like utf8 is the letter k? Pressing shift id supposed to insert the letter k?

@kovidgoyal
Copy link
Owner

This is not making any sense to me. Can you give me a set of steps to reproduce the problem with some builtin IME available on macOS

@page-down
Copy link
Contributor

page-down commented Jan 20, 2022

Yes, it's the k in the log.

According to my understanding, after the input method is canceled (cancelComposition), the client's insertText will be called, which is handled by the client itself. Of course, due to our current implementation, the insertText is not sent to glfw after it is received. (The reason why to do so is also obvious)

Also I was recently looking at whether there is a relevant implementation in QT and came across the following code and wondered if it would help.

// qtbase.git/tree/src/plugins/platforms/cocoa/qnsview.mm
QInputMethodQueryEvent queryEvent(Qt::ImEnabled | Qt::ImHints);
            if (QCoreApplication::sendEvent(fo, &queryEvent)) {
                bool imEnabled = queryEvent.value(Qt::ImEnabled).toBool();

Can you give me a set of steps to reproduce the problem with some builtin IME available on macOS

I can give you the configuration example for RIME Squirrel.
Simple enough to test this.
Although it is not a built-in input method.

default.custom.yaml

patch:
  schema_list:
    - schema: table-auto-select
  ascii_composer/switch_key:
    # commit_text - the selected candidate text
    # commit_code - the code
    # clear - cancel and clear all without commit
    Shift_L: commit_code
  key_binder/bindings: {}
  switcher/hotkeys: {}

The rest is the same as the previous one.
#4219 (comment)

After changing the configuration file, you need to click the input method icon in the menu bar and select Deploy.

Type k and press shift.

@page-down
Copy link
Contributor

The following is the relevant input method controller implementation, the rest are not important.

https://github.com/rime/squirrel/blob/master/SquirrelInputController.m

@implementation SquirrelInputController { ... }
- (BOOL)handleEvent:(NSEvent*)event client:(id)sender
{
  // Return YES to indicate the the key input was received and dealt with.
  // Key processing will not continue in that case.  In other words the
  // system will not deliver a key down event to the application.
  // Returning NO means the original key down will be passed on to the client.

  // ...
    switch (event.type) {
      case NSEventTypeFlagsChanged: {
        if (_lastModifier == modifiers) {
          handled = YES;
          break;
        // ...
  return handled;
}

-(void)deactivateServer:(id)sender
{
  //...
  [self commitComposition:sender];
}

-(void)commitComposition:(id)sender
{
  //...
      // [self commitString: @(raw_input)];
      // The code in commitString is:
        [_currentClient insertText:string
            replacementRange:NSMakeRange(NSNotFound, 0)];
        _preeditString = @"";
}

@kovidgoyal
Copy link
Owner

Hmm, well in that case the only way to fix this is to actually implement storage of the markedText and commit when the marking is removed. That will neccessitate a rewrite of a whole lot of keyDown and insertText and various other things. Something for a rainy day.

@kovidgoyal
Copy link
Owner

I committed a fix with a slightly different approach. See if it works,
as I cant be bothered setting up RIME to test this.

@page-down
Copy link
Contributor

No problem, I can help test it.

the only way to fix this is to actually implement storage of the markedText and commit when the marking is removed.
That will neccessitate a rewrite of a whole lot of keyDown and insertText and various other things.

I think it's indeed as you said that the key event input needs to be sorted out. Since the input source is actually turned on the whole time and only works properly after letting the input method handle everything.

The problem still exists with the following logs.

---------------- key down -------------------
NSEvent: type=KeyDown loc=(273.984,695.016) time=30204.5 flags=0x100 win=0x7fb1b82a0a30 winNum=1588 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Press: native_key: 0x28 (k) glfw_key: 0x6b mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: k selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: k marked_text: (k)
on_key_input: glfw key: 0x6b native_code: 0x28 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
on_key_input: glfw key: 0x6b native_code: 0x28 action: REPEAT mods: none text: 'k' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(273.984,695.016) time=30204.6 flags=0x100 win=0x7fb1b82a0a30 winNum=1588 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Release: native_key: 0x28 (k) glfw_key: 0x6b mods: none 
on_key_input: glfw key: 0x6b native_code: 0x28 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(273.984,695.016) time=30207.6 flags=0x20102 win=0x7fb1b82a0a30 winNum=1588 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000

	insertText: k replacementRange: (9223372036854775807, 0)
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(273.984,695.016) time=30207.7 flags=0x100 win=0x7fb1b82a0a30 winNum=1588 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
---------------- key down -------------------
NSEvent: type=KeyDown loc=(273.984,695.016) time=30211.2 flags=0x100 win=0x7fb1b82a0a30 winNum=1588 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Press: native_key: 0x22 (i) glfw_key: 0x69 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: i replacementRange: (9223372036854775807, 0)
text: 0x69 glfw_key: i marked_text: ()
on_key_input: glfw key: 0x69 native_code: 0x22 action: PRESS mods: none text: 'i' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
sent key as text to child
Received input from child (screen_draw) while overlay active. Overlay contents: k
updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
...
// <----- At this point, k pre edit text is displayed after i.
----------------- key up --------------------
NSEvent: type=KeyUp loc=(273.984,695.016) time=30211.3 flags=0x100 win=0x7fb1b82a0a30 winNum=1588 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Release: native_key: 0x22 (i) glfw_key: 0x69 mods: none 
on_key_input: glfw key: 0x69 native_code: 0x22 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000

@kovidgoyal
Copy link
Owner

why is CLEAR_PRE_EDIT_TEXT not being called in flagsChanged for you? Is
process_text not true?

@page-down
Copy link
Contributor

After [inpctx handleEvent:event]
marked_text_cleared_by_insert == false

@kovidgoyal
Copy link
Owner

But in your log insertText is being called. Can you use a debugger to figure out what is the call stack that is calling it?

@page-down
Copy link
Contributor

Is it related to the following logs? I'm still working on other things and need a little time.
I will try something based on this idea. Please let me know if you have any other thoughts.

-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(258.984,689.016) time=31665.3 flags=0x20102 win=0x7fc5f3d6c450 winNum=1735 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 
- ---------> process_text 1 // before if (process_text && inpctx)
- ---------> marked_text_cleared_by_insert 0 // after [inpctx handleEvent:event]

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

	insertText: k replacementRange: (9223372036854775807, 0)

@kovidgoyal
Copy link
Owner

Yes basically, I expect the call to handleEvent to in turn call inserttext which should set marked_text_cleared_by_insert. From your log it looks like insertText is called by something else, unless I am reading the log wrong. And no rush take your time, I am anyway going to be ending for the day soon.

@page-down
Copy link
Contributor

OK, so I'll send the stack trace first.

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.41
  * frame #0: 0x0000000103e11f30 glfw-cocoa.so`-[GLFWContentView insertText:replacementRange:]
    frame #1: 0x00007ff81d0ba772 AppKit`-[NSTextInputContext(NSInputContext_WithCompletion) insertText:replacementRange:completionHandler:] + 176
    frame #2: 0x00007ff81d867f7d AppKit`__55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2.354 + 102
    frame #3: 0x00007ff81d0ba6ba AppKit`-[NSTextInputContext do_HandleTSMEvent_insertFixLenTextLoop:whileCondition:dispatchWorkEach:afterEachInsertText:continuation:] + 105
    frame #4: 0x00007ff81d0ba581 AppKit`-[NSTextInputContext tryHandleTSMEvent_insertFixLenText_withContext:dispatchCondition:setupForDispatch:nestedWorkaroundCondition:nestedWorkaroundDispatchWork:loopCondition:dispatchWorkEach:afterEachInsertText:continuation:] + 310
    frame #5: 0x00007ff81d867971 AppKit`__55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke.310 + 3028
    frame #6: 0x00007ff81d0ba1a8 AppKit`__55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2 + 74
    frame #7: 0x00007ff81d0ba12e AppKit`-[NSTextInputContext tryHandleTSMEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 87
    frame #8: 0x00007ff81d0b9689 AppKit`-[NSTextInputContext handleTSMEvent:completionHandler:] + 1859
    frame #9: 0x00007ff81d0b8ed8 AppKit`_NSTSMEventHandler + 299
    frame #10: 0x00007ff823088d1d HIToolbox`DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) + 1391
    frame #11: 0x00007ff82308814e HIToolbox`SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) + 333
    frame #12: 0x00007ff823087fef HIToolbox`SendEventToEventTargetWithOptions + 45
    frame #13: 0x00007ff8230e3666 HIToolbox`SendTSMEvent_WithCompletionHandler + 381
    frame #14: 0x00007ff823291d3e HIToolbox`__SendTextInputEvent_WithCompletionHandler_block_invoke + 486
    frame #15: 0x00007ff8232905ca HIToolbox`SendTextInputEvent_WithCompletionHandler + 1126
    frame #16: 0x00007ff8232f1610 HIToolbox`-[IMKInputSession _postEvent:completionHandler:] + 156
    frame #17: 0x00007ff823302d68 HIToolbox`-[IMKInputSession insertText:replacementRange:completionHandler:] + 296
    frame #18: 0x00007ff823303635 HIToolbox`-[IMKInputSession insertText:replacementRange:validFlags:completionHandler:] + 96
    frame #19: 0x00007ff8232f24b5 HIToolbox`__71-[IMKInputSession imkxpc_insertText:replacementRange:validFlags:reply:]_block_invoke + 559
    frame #20: 0x00007ff81a47e4a3 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #21: 0x00007ff81a47e353 CoreFoundation`__CFRunLoopDoBlocks + 445
    frame #22: 0x00007ff81a47d6b8 CoreFoundation`__CFRunLoopRun + 2586
    frame #23: 0x00007ff81a47c5dd CoreFoundation`CFRunLoopRunSpecific + 563
    frame #24: 0x00007ff8230af4f1 HIToolbox`RunCurrentEventLoopInMode + 292
    frame #25: 0x00007ff8230af247 HIToolbox`ReceiveNextEventCommon + 587
    frame #26: 0x00007ff8230aefe5 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 70
    frame #27: 0x00007ff81ceabb4c AppKit`_DPSNextEvent + 886
    frame #28: 0x00007ff81ceaa1b8 AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1411
    frame #29: 0x00007ff81ce9c5a9 AppKit`-[NSApplication run] + 586
    frame #30: 0x0000000103dfdc2a glfw-cocoa.so`glfwRunMainLoop + 90

@kovidgoyal
Copy link
Owner

Ah, its being called by the event loop not by handleEvent, so this approach wont work.

@kovidgoyal kovidgoyal reopened this Jan 22, 2022
@kovidgoyal
Copy link
Owner

Try 655a5a2

@page-down
Copy link
Contributor

Finally, the pre edit text can be cleared (sometimes only), even though the text is not inserted into the child.
(somewhere in the log is missing '\n')

---------------- key down -------------------
NSEvent: type=KeyDown loc=(-90.0156,299.016) time=33450.1 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Press: native_key: 0x28 (k) glfw_key: 0x6b mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: k selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: k marked_text: (k)
on_key_input: glfw key: 0x6b native_code: 0x28 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
on_key_input: glfw key: 0x6b native_code: 0x28 action: REPEAT mods: none text: 'k' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(-90.0156,299.016) time=33450.3 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Release: native_key: 0x28 (k) glfw_key: 0x6b mods: none 
on_key_input: glfw key: 0x6b native_code: 0x28 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(-90.0156,299.016) time=33450.9 flags=0x20102 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

	insertText: k replacementRange: (9223372036854775807, 0)
clearing pre-edit because insertText called from event loop
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: '' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
sending text to kitty from insertText called from event loop: kon_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: 'k' state: 0 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
ignoring release event for previous press that was handled as shortcut
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(-90.0156,299.016) time=33451.0 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

The following is the log when it is not working.

-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33589.5 flags=0x20102 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

	insertText: k replacementRange: (9223372036854775807, 0)
clearing pre-edit because insertText called from event loop
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: '' state: 1 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
sending text to kitty from insertText called from event loop: kon_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: 'k' state: 0 updateIMEPosition: left=290.000000, top=34.000000, width=8.000000, height=17.000000
ignoring release event for previous press that was handled as shortcut
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33589.7 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=290.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33589.8 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="w" unmodchars="w" repeat=0 keyCode=13
Press: native_key: 0xd (w) glfw_key: 0x77 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: w replacementRange: (9223372036854775807, 0)
text: 0x77 glfw_key: w marked_text: ()
on_key_input: glfw key: 0x77 native_code: 0xd action: PRESS mods: none text: 'w' state: 0 updateIMEPosition: left=290.000000, top=34.000000, width=8.000000, height=17.000000
sent key as text to child
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33589.9 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="w" unmodchars="w" repeat=0 keyCode=13
Release: native_key: 0xd (w) glfw_key: 0x77 mods: none 
on_key_input: glfw key: 0x77 native_code: 0xd action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33592.3 flags=0x20102 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33592.4 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33592.6 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Press: native_key: 0x28 (k) glfw_key: 0x6b mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: k selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: k marked_text: (k)
on_key_input: glfw key: 0x6b native_code: 0x28 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
on_key_input: glfw key: 0x6b native_code: 0x28 action: REPEAT mods: none text: 'k' state: 1 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33592.8 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Release: native_key: 0x28 (k) glfw_key: 0x6b mods: none 
on_key_input: glfw key: 0x6b native_code: 0x28 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33593.2 flags=0x20102 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

	insertText: k replacementRange: (9223372036854775807, 0)
clearing pre-edit because insertText called from event loop
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: '' state: 1 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
sending text to kitty from insertText called from event loop: kon_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: 'k' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
ignoring release event for previous press that was handled as shortcut
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33593.4 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33593.5 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Press: native_key: 0x22 (i) glfw_key: 0x69 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: i replacementRange: (9223372036854775807, 0)
text: 0x69 glfw_key: i marked_text: ()
on_key_input: glfw key: 0x69 native_code: 0x22 action: PRESS mods: none text: 'i' state: 0 updateIMEPosition: left=298.000000, top=34.000000, width=8.000000, height=17.000000
sent key as text to child
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33593.6 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Release: native_key: 0x22 (i) glfw_key: 0x69 mods: none 
on_key_input: glfw key: 0x69 native_code: 0x22 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33594.2 flags=0x20102 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33594.3 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33594.4 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Press: native_key: 0x28 (k) glfw_key: 0x6b mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: k selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: k marked_text: (k)
on_key_input: glfw key: 0x6b native_code: 0x28 action: PRESS mods: none text: 'k' state: 1 updateIMEPosition: left=306.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
on_key_input: glfw key: 0x6b native_code: 0x28 action: REPEAT mods: none text: 'k' state: 1 updateIMEPosition: left=314.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'k'
Ignoring key press as IME is active and it generated no text
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33594.5 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="w" unmodchars="w" repeat=0 keyCode=13
Press: native_key: 0xd (w) glfw_key: 0x77 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: kw selectedRange: (2, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: w marked_text: (kw)
on_key_input: glfw key: 0x77 native_code: 0xd action: PRESS mods: none text: 'kw' state: 1 updateIMEPosition: left=314.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'kw'
on_key_input: glfw key: 0x77 native_code: 0xd action: REPEAT mods: none text: 'kw' state: 1 updateIMEPosition: left=322.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'kw'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33594.6 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="k" unmodchars="k" repeat=0 keyCode=40
Release: native_key: 0x28 (k) glfw_key: 0x6b mods: none 
on_key_input: glfw key: 0x6b native_code: 0x28 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=322.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33594.6 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="a" unmodchars="a" repeat=0 keyCode=0
Press: native_key: 0x0 (a) glfw_key: 0x61 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: kwa selectedRange: (3, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: a marked_text: (kwa)
on_key_input: glfw key: 0x61 native_code: 0x0 action: PRESS mods: none text: 'kwa' state: 1 updateIMEPosition: left=322.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'kwa'
on_key_input: glfw key: 0x61 native_code: 0x0 action: PRESS mods: none text: 'kwa' state: 1 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
updated pre-edit text: 'kwa'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33594.7 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="w" unmodchars="w" repeat=0 keyCode=13
Release: native_key: 0xd (w) glfw_key: 0x77 mods: none 
on_key_input: glfw key: 0x77 native_code: 0xd action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33594.7 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="a" unmodchars="a" repeat=0 keyCode=0
Release: native_key: 0x0 (a) glfw_key: 0x61 mods: none 
on_key_input: glfw key: 0x61 native_code: 0x0 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33595.1 flags=0x20102 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: shift 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: shift text: '' state: 0 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

	setMarkedText:  selectedRange: (0, 0) replacementRange: (9223372036854775807, 0)

	insertText: kwa replacementRange: (9223372036854775807, 0)
sending text to kitty from insertText called from event loop: kwaon_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: 'kwa' state: 0 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
ignoring release event for previous press that was handled as shortcut
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33595.2 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=56
flagsChanged: modifer: shift native_key: 0x38 (<noname>) glfw_key: 0xe061 mods: none 

on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33595.3 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="f" unmodchars="f" repeat=0 keyCode=3
Press: native_key: 0x3 (f) glfw_key: 0x66 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: f replacementRange: (9223372036854775807, 0)
text: 0x66 glfw_key: f marked_text: ()
on_key_input: glfw key: 0x66 native_code: 0x3 action: PRESS mods: none text: 'f' state: 0 updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
sent key as text to child
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=330.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_draw) while overlay active. Overlay contents: kwa
updateIMEPosition: left=338.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=338.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=338.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_carriage_return) while overlay active. Overlay contents: kwa
updateIMEPosition: left=26.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_cursor_back) while overlay active. Overlay contents: kwa
updateIMEPosition: left=338.000000, top=34.000000, width=8.000000, height=17.000000
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33595.4 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="w" unmodchars="w" repeat=0 keyCode=13
Press: native_key: 0xd (w) glfw_key: 0x77 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: w replacementRange: (9223372036854775807, 0)
text: 0x77 glfw_key: w marked_text: ()
on_key_input: glfw key: 0x77 native_code: 0xd action: PRESS mods: none text: 'w' state: 0 updateIMEPosition: left=338.000000, top=34.000000, width=8.000000, height=17.000000
sent key as text to child
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=338.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_draw) while overlay active. Overlay contents: kwa
updateIMEPosition: left=346.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_carriage_return) while overlay active. Overlay contents: kwa
updateIMEPosition: left=26.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_cursor_back) while overlay active. Overlay contents: kwa
updateIMEPosition: left=346.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=346.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=346.000000, top=34.000000, width=8.000000, height=17.000000
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33595.5 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="a" unmodchars="a" repeat=0 keyCode=0
Press: native_key: 0x0 (a) glfw_key: 0x61 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: a replacementRange: (9223372036854775807, 0)
text: 0x61 glfw_key: a marked_text: ()
on_key_input: glfw key: 0x61 native_code: 0x0 action: PRESS mods: none text: 'a' state: 0 updateIMEPosition: left=346.000000, top=34.000000, width=8.000000, height=17.000000
sent key as text to child
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=346.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_draw) while overlay active. Overlay contents: kwa
updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_carriage_return) while overlay active. Overlay contents: kwa
updateIMEPosition: left=26.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_cursor_back) while overlay active. Overlay contents: kwa
updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33595.5 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="f" unmodchars="f" repeat=0 keyCode=3
Release: native_key: 0x3 (f) glfw_key: 0x66 mods: none 
on_key_input: glfw key: 0x66 native_code: 0x3 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
---------------- key down -------------------
NSEvent: type=KeyDown loc=(809.984,220.016) time=33595.5 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Press: native_key: 0x22 (i) glfw_key: 0x69 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	insertText: i replacementRange: (9223372036854775807, 0)
text: 0x69 glfw_key: i marked_text: ()
on_key_input: glfw key: 0x69 native_code: 0x22 action: PRESS mods: none text: 'i' state: 0 updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
sent key as text to child
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=354.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_draw) while overlay active. Overlay contents: kwa
updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_carriage_return) while overlay active. Overlay contents: kwa
updateIMEPosition: left=26.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (screen_cursor_back) while overlay active. Overlay contents: kwa
updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
Received input from child (select_graphic_rendition) while overlay active. Overlay contents: kwa
updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33595.6 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="w" unmodchars="w" repeat=0 keyCode=13
Release: native_key: 0xd (w) glfw_key: 0x77 mods: none 
on_key_input: glfw key: 0x77 native_code: 0xd action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33595.6 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="a" unmodchars="a" repeat=0 keyCode=0
Release: native_key: 0x0 (a) glfw_key: 0x61 mods: none 
on_key_input: glfw key: 0x61 native_code: 0x0 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
----------------- key up --------------------
NSEvent: type=KeyUp loc=(809.984,220.016) time=33595.7 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Release: native_key: 0x22 (i) glfw_key: 0x69 mods: none 
on_key_input: glfw key: 0x69 native_code: 0x22 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33598.1 flags=0x100108 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=55
flagsChanged: modifer: super native_key: 0x37 (<noname>) glfw_key: 0xe064 mods: super 

on_key_input: glfw key: 0xe064 native_code: 0x37 action: PRESS mods: super text: '' state: 0 updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(809.984,220.016) time=33598.2 flags=0x100 win=0x7f96571cad20 winNum=1835 ctxt=0x0 keyCode=55
flagsChanged: modifer: super native_key: 0x37 (<noname>) glfw_key: 0xe064 mods: none 

on_key_input: glfw key: 0xe064 native_code: 0x37 action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=362.000000, top=34.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

@kovidgoyal
Copy link
Owner

And another: f21ddae

@page-down
Copy link
Contributor

After a very short time of testing, the preedit text can be cleaned up and no noticeable problems are found.

Except the following doesn't seem to work.
There is no k in child.

sending text to kitty from insertText called from event loop: k

@kovidgoyal
Copy link
Owner

I thought my commit took care of that. Check what's happening in
on_key_input in keys.c.

@page-down
Copy link
Contributor

page-down commented Jan 22, 2022

if (size == SEND_TEXT_TO_CHILD) {
    // ...
} else if (size > 0) {
    // ...
} else {
    debug("ignoring ...");
}
sending text to kitty from insertText called from event loop: k
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: 'k' state: 0 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
--> size: 0 ev->text: k
ignoring as keyboard mode does not support encoding this event

size: 0 ev->text: k

It seems that k cannot be encoded?

EDIT3:

encode_key:
if (!ev->report_all_event_types && ev->action == RELEASE) return 0;

EDIT4:

Try the following code, I can insert the text now.

GLFWkeyevent glfw_keyevent = {.text=_glfw.ns.text, .ime_state=GLFW_IME_COMMIT_TEXT};

EDIT5:

I see some REPEAT during normal input.

---------------- key down -------------------
NSEvent: type=KeyDown loc=(123.984,408.016) time=39934.0 flags=0x100 win=0x7fc6fb128310 winNum=2440 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Press: native_key: 0x22 (i) glfw_key: 0x69 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: ki selectedRange: (2, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: i marked_text: (ki)
on_key_input: glfw key: 0x69 native_code: 0x22 action: PRESS mods: none text: 'ki' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'ki'
on_key_input: glfw key: 0x69 native_code: 0x22 action: REPEAT mods: none text: 'ki' state: 1 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'ki'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(123.984,408.016) time=39934.1 flags=0x100 win=0x7fc6fb128310 winNum=2440 ctxt=0x0 chars="i" unmodchars="i" repeat=0 keyCode=34
Release: native_key: 0x22 (i) glfw_key: 0x69 mods: none

@page-down
Copy link
Contributor

Since we're already dealing with IME issues, here's the last one I know of, for now.

  • Switch to the built-in Japanese Romaji input method. (Take this as an example)
  • Enter ro
  • In the input state, press Ctrl+L (full) Ctrl+; (half) to switch different letter modes. (still using this as an example)
  • At this point the preedit text is mistakenly cleared. (The text updated by IME should be displayed.)
---------------- key down -------------------
NSEvent: type=KeyDown loc=(231.984,694.016) time=38857.5 flags=0x100 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 chars="r" unmodchars="r" repeat=0 keyCode=15
Press: native_key: 0xf (r) glfw_key: 0x72 mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: r selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: r marked_text: (r)
on_key_input: glfw key: 0x72 native_code: 0xf action: PRESS mods: none text: 'r' state: 1 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'r'
on_key_input: glfw key: 0x72 native_code: 0xf action: REPEAT mods: none text: 'r' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'r'
Ignoring key press as IME is active and it generated no text
---------------- key down -------------------
NSEvent: type=KeyDown loc=(231.984,694.016) time=38857.6 flags=0x100 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 chars="o" unmodchars="o" repeat=0 keyCode=31
Press: native_key: 0x1f (o) glfw_key: 0x6f mods: none char_count: 1 deadKeyState: 0 repeat: 0 
	setMarkedText: ろ selectedRange: (1, 0) replacementRange: (9223372036854775807, 0)
text: <none> glfw_key: o marked_text: (ろ)
on_key_input: glfw key: 0x6f native_code: 0x1f action: PRESS mods: none text: 'ろ' state: 1 updateIMEPosition: left=298.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'ろ'
on_key_input: glfw key: 0x6f native_code: 0x1f action: REPEAT mods: none text: 'ろ' state: 1 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: 'ろ'
Ignoring key press as IME is active and it generated no text
----------------- key up --------------------
NSEvent: type=KeyUp loc=(231.984,694.016) time=38857.7 flags=0x100 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 chars="r" unmodchars="r" repeat=0 keyCode=15
Release: native_key: 0xf (r) glfw_key: 0x72 mods: none 
on_key_input: glfw key: 0x72 native_code: 0xf action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
----------------- key up --------------------
NSEvent: type=KeyUp loc=(231.984,694.016) time=38857.7 flags=0x100 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 chars="o" unmodchars="o" repeat=0 keyCode=31
Release: native_key: 0x1f (o) glfw_key: 0x6f mods: none 
on_key_input: glfw key: 0x6f native_code: 0x1f action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(231.984,694.016) time=38862.1 flags=0x40101 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 keyCode=59
flagsChanged: modifer: ctrl native_key: 0x3b (<noname>) glfw_key: 0xe062 mods: ctrl 

on_key_input: glfw key: 0xe062 native_code: 0x3b action: PRESS mods: ctrl text: '' state: 0 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

	setMarkedText: ro selectedRange: (2, 0) replacementRange: (9223372036854775807, 0)
updating IME text in kitty from setMarkedText called from event loop: 
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: '' state: 1 updateIMEPosition: left=306.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
----------------- key up --------------------
NSEvent: type=KeyUp loc=(231.984,694.016) time=38862.2 flags=0x40101 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 chars="
                                                                                                                    " unmodchars="l" repeat=0 keyCode=37
Release: native_key: 0x25 (l) glfw_key: 0x6c mods: ctrl 

	setMarkedText: ro selectedRange: (2, 0) replacementRange: (9223372036854775807, 0)
updating IME text in kitty from setMarkedText called from event loop: 
on_key_input: glfw key: 0x0 native_code: 0x0 action: RELEASE mods: none text: '' state: 1 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
updated pre-edit text: ''
----------------- key up --------------------
NSEvent: type=KeyUp loc=(231.984,694.016) time=38865.3 flags=0x40101 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 chars=";" unmodchars=";" repeat=0 keyCode=41
Release: native_key: 0x29 (;) glfw_key: 0x3b mods: ctrl 
-------------- flags changed -----------------
NSEvent: type=FlagsChanged loc=(231.984,694.016) time=38865.5 flags=0x100 win=0x7fd27bad3770 winNum=2241 ctxt=0x0 keyCode=59
flagsChanged: modifer: ctrl native_key: 0x3b (<noname>) glfw_key: 0xe062 mods: none 

on_key_input: glfw key: 0xe062 native_code: 0x3b action: RELEASE mods: none text: '' state: 0 updateIMEPosition: left=290.000000, top=17.000000, width=8.000000, height=17.000000
ignoring as keyboard mode does not support encoding this event

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 22, 2022 via email

@page-down
Copy link
Contributor

page-down commented Jan 22, 2022

size: 0 ev->text: k It seems that k cannot be encoded?

ff836a8

Sorry, I edited the above reply. Is it possible to use:

GLFWkeyevent glfw_keyevent = {.text=_glfw.ns.text, .ime_state=GLFW_IME_COMMIT_TEXT};

At this point, when using the modifier key, it clears the preedit text and inserts the text properly, and no other problems were found except for the REPEAT key event mentioned above.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Jan 22, 2022 via email

@kovidgoyal
Copy link
Owner

Never mind I committed the change.

@kovidgoyal
Copy link
Owner

I believe this is fixed now, closing.

@skysky97
Copy link

@kovidgoyal
There is a similar problem on nightly version when I use ibus-libpinyin with its compact display style.

To Reproduce:

  1. Type some words, the input characters are shown on kitty window, the candidates are shown on the single line pane.
  2. Type Backspace and delete all input characters, candidates pane is disappeared, but there are some characters left on kitty window.
  3. When I switch to another window and switch back, or just type ENTER, then the characters are disappeared.

@kovidgoyal
Copy link
Owner

2151969

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants