Tk Source Code

View Ticket
Login
Ticket UUID: 585584ad6668af39eb829adcd182016531455cc7
Title: Aqua: `event generate` not working for function keys
Type: Bug Version: 8.6.9.1
Submitter: chrstphrchvz Created on: 2019-08-21 10:47:09
Subsystem: 69. Events Assigned To: marc_culler
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2020-05-12 15:15:57
Resolution: Fixed Closed By: marc_culler
    Closed on: 2020-05-12 15:15:57
Description:

Function key events generated using event generate are not properly generated on Aqua.

For example, using event generate … <Key-F1> or event generate … <Key> -keysym F1, the resulting keysym (%K) is ?? rather than F1, and the event will not trigger any <Key-F1> bindings. (The keycode %k will also not have the exact same value as an actual keypress, e.g. 0x7a0010 rather than 0x7af704; but I do not know if that is relevant to this issue.)

User Comments: marc_culler (claiming to be Marc Culler) added on 2020-05-12 15:15:57:
The new key event handling code has been merged into core-8-6-branch and
trunk so I am closing this ticket.

nab added on 2020-05-05 16:31:56:
ok, will do.
thanks

marc_culler (claiming to be Marc Culler) added on 2020-05-05 16:16:41:
This is a very interesting issue, but it does not have anything to do with
key events.  It is probably mostly about drawRect.  How about posting your
script in ticket [06d8246baf7d]?

nab added on 2020-05-05 15:08:54:
Hi Marc,
just a side note here but please tell me if it's out of topic...
using this script:
package require Tk
package require Ttk

ttk::button .b -text push
grid .b -sticky news

for {set x 1} {$x<100} {incr x} {
    canvas .c$x -width 1 -height 1 -bg black
    grid .c$x -sticky news
}

grid columnconfigure . all -weight 1
grid rowconfigure . 1 -weight 1


bind .b <ButtonPress-1> {doColorchange red}
bind .b <ButtonRelease-1> {doColorchange yellow}

proc doColorchange {colour} {
    for {set x 1} {$x<100} {incr x} {
        after $x [list .c$x configure -bg $colour]
     }
}
#eof


if you <ButtonPress-1> on the push button, each canvas goes red but when you <ButtonRelease-1>, not all canvas goes to yellow (at least not all of them).
mac_styles branch also behave like that.

using mac_styles branch with the patch of Christopher Chavez provided in this ticket :
https://core.tcl-lang.org/tk/info/06d8246baf7d0009
all canvas always goes to the right colours.

with this branch and original mac_styles branch not all events are processed only when <ButtonRelease-1>
<ButtonPress-1> always change canvas colour.

I'm aware it may be not completely related to this ticket as it's more about <ButtonRelease-1> but I thought you should know.

++

marc_culler (claiming to be Marc Culler) added on 2020-05-05 12:24:07:
Thank you, Nicolas.  That sounds very encouraging.

I am aware of the "Window move completed without beginning" message when
you select a letter from the popup menu by clicking.  That was present before
these changes as well.  It is annoying, but seemingly harmless.  I have not
been able to find where in the Apple code it comes from or why. I am not able
to assign any reasonable meaning to those words since no window is being moved
when you click on a letter.  The message is mentioned sporadically on the
internet, but never explained anywhere that I have seen.

nab added on 2020-05-05 09:46:00:
Hi Marc,
I've tested the branch of this ticket and didn't noticed anything wrong... In my app there's <F1> binding and it works as expected.
In ttk::entry, everything works, the only thing I've noticed is a message in the terminal (I've launched my app from the terminal for testing) when I press 'a' key for a long time.
long press pop-up the alt panel, if I choose the alt letter by pressing numbers it's fine but if I use mouse to choose the alt letter, the message in the terminal is:
2020-05-05 11:27:23.023 DLight[29710:3289396] Warning: Window move completed without beginning

the alt letter is displayed in the entry though...

menuBar menu accelerator works fine (I mean Command-Q, or Command-W for example).

<Return> binding works as expected on menu posted by ttk::combobox

the only complex binding I have is <Control-F1> which works as expected

I didn't test text widget

best regards,

Nicolas

marc_culler (claiming to be Marc Culler) added on 2020-05-05 00:55:55:
I think the bugfix branch is now complete, including non-regression tests.  Of
course there is still one more bug -- there always is -- so it would be really
great if people could review it one more time and tell me what the bug is.  Also,
I would welcome ideas for other non-regression tests.

This was almost a complete rewrite of the key event processing.  So it does need
some serious testing before we merge the branch.

marc_culler (claiming to be Marc Culler) added on 2020-04-30 12:22:25:
The reason that the virtual keycode is missing from KeyPress events in a text
or entry widget is that those events are not created by translating an NSEvent
into an XEvent.  Rather, they are synthesized by the NSTextInputClient when
it finishes interpreting a composition sequence.  Of course a composition
sequence could consist of just a single keypress, but it might also have
occurred after the user did a press-and-hold and then selected a character
from the popup menu. KeyRelease events are not interpreted by the
NSTextInputClient.

I am thinking that an XEvent which is synthesized by the NSTextInputClient
should be assigned the special NO_VIRTUAL value, or a different special value
specific to those events.  At some point I realized that 0 is a bad
choice since, as you mention, it actually is a valid virtual keycode, and
that also means that 0 should not be used as the virtual keycode for the
events synthesized by the NSTextInputClient. We have 8 bits and only 128
virtual keycodes so there are plenty of values available.

I have been testing the same way as you, but you are correct that the purpose
of the injectkeyevent command is to do testing more systematically.  I don't
know yet whether the non-regression test should just run through all 512
combinations of shift-option-virtual or make some careful choice of
representative key events.  If you have some suggestions I am all ears.

chrstphrchvz added on 2020-04-30 09:06:28:

Something else I notice (not introduced by any changes on bug-585584ad66): keypress (not keyrelease) events in text/entry widgets for many keys do not contain the virtual keycode in the higher bits of %k. Is this expected?

I think to avoid playing whack-a-mole, some standard tests are needed, if there aren't already any; I would guess the new injectkeyevent command is an effort toward this (though I wonder if it's also possible to test using AppleScript, if that gives enough control and helps ensure simulated events are well-formed/match actual events). So far I've relied on doing

bind all <KeyPress>/<KeyRelease> {
    puts [format {%%s %%x %%s %%x %%b} %A %k %K %N %s]
}
and comparing what happens and gets output for actual and generated events for both text/entry widgets and other types of widgets.


chrstphrchvz added on 2020-04-30 07:03:50:

Pressing A (virtual keycode 1)…

Sorry, the A key has virtual keycode 0.


chrstphrchvz added on 2020-04-30 06:36:14:

Adding the missing braces avoids the crash.

The easiest way to trigger the crash was to open wish, focus on the blank Tk window, and press A.

For typing into an entry or text widget (such as any in the widget demo), at most two inserting keypresses were needed to cause the crash. I didn't find a reliable way to crash using actual mousewheel events, but doing event generate . <MouseWheel> -delta 0 twice was enough to cause the crash.

Why mousewheel events (and not activate/deactivate, it turns out) could also cause the crash: Tk_BindEvent() (tkBind.c) treats mousewheel-type events as having the KEY flag (from flagArray[]), and so fills in curEvent->detail.info from TkpGetKeySym(). I don't think there's any point to looking up keycodes/keysyms for mousewheel events (various other uses of (flags & KEY) simultaneously check for MouseWheelEvent type); I have proposed a patch to address that in [d2a10a14a8].


marc_culler (claiming to be Marc Culler) added on 2020-04-30 01:34:27:
Thank you Christopher!  Yes, I had meant for there to be braces around that
block.  I just added them. Let's see if you can still cause the crash with a
mousewheel event.

I am surprised that I didn't see that crash.  How did you trigger it, exactly?

chrstphrchvz added on 2020-04-30 01:12:01:

In [45d669d2ea], tkMacOSXKeyboard.c line 434-436: this block is not controlled by if (virtual == NO_VIRTUAL) (missing braces?), meaning an uninitialized value of hPtr can get used, causing a crash. Pressing A (virtual keycode 1) anywhere or typing into a text/entry widget are reliable triggers; but I notice this crash can also happen for non-key events, e.g. mousewheel scrolling events (possibly also activate/deactivate events—not sure), so I wonder if something is not handled properly in Tk_BindEvent() for certain event types/flags or if those events' fields are not initialized properly by whatever queues them.

Stack trace:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   wish8.6                       	0x00000001021611b4 XKeycodeToKeysym + 196 (tkMacOSXKeyboard.c:435)
1   wish8.6                       	0x0000000102161b77 TkpGetKeySym + 359 (tkMacOSXKeyboard.c:804)
2   wish8.6                       	0x0000000102036ce3 Tk_BindEvent + 2099 (tkBind.c:2332)
3   wish8.6                       	0x0000000102045a18 TkBindEventProc + 760 (tkCmds.c:319)
4   wish8.6                       	0x0000000102050b78 Tk_HandleEvent + 728
5   wish8.6                       	0x00000001020518be WindowEventProc + 206 (tkEvent.c:1724)
6   wish8.6                       	0x00000001022fed2a Tcl_ServiceEvent + 202 (tclNotify.c:670)
7   wish8.6                       	0x00000001022ff22b Tcl_DoOneEvent + 523 (tclNotify.c:967)
8   wish8.6                       	0x0000000102051eae Tk_MainLoop + 30 (tkEvent.c:2106)
9   wish8.6                       	0x0000000102067397 Tk_MainEx + 1671 (tkMain.c:380)
10  wish8.6                       	0x00000001020304ae main + 78 (tkAppInit.c:78)
11  libdyld.dylib                 	0x00007fff6776fcc9 start + 1


marc_culler (claiming to be Marc Culler) added on 2020-04-29 19:10:36:

I think that all of these issues have been dealt with in the latest commit to bug-585584ad66 (45d669d2ea).

I was using 0xF8FF (the last code point in the private use range) as the keychar for modifier KeyPress events, naively assuming that the last code point was least likely to be used by Apple. It turns out they use it for their logo. Sigh.

There needs to be a special value in the keycode for modifier KeyPress XEvents because there is no characters attribute for an NSEvent of type FlagsChanged, which is what Apple uses to report modifier key presses or releases.


chrstphrchvz added on 2020-04-29 03:36:26:

One final key combination I'm aware of is the Apple-specific option-shift-K easter egg  (U+F8FF). It seems possible to use with:

#define IS_PRINTABLE(keychar) … || (keychar == 0xF8FF)

(Since there's probably no X11 keysym for this character, I don't expect to be able to generate an event for it.)


chrstphrchvz added on 2020-04-28 20:38:23:

I had actually observed the crash using just the blank Wish window and holding e.g. Q—not necessarily press-and-hold keys. The fix works nonetheless.

Some issues I observe, which appear to be introduced by [95c18e3b64]:

  • The state (%s) may contain garbage, since int *state in XKeysymToKeycodeWithState() and int state in TkpSetKeycodeAndState() are uninitialized for generated key events (without having specified -state) for keysyms which are not both defined in keysym2unichar and whose characters are defined in unichar2virtual. The garbage is interpreted as randomly-set modifier bits: an example is doing event generate $wid <Key-Left>/<Key-Right>) in a text/entry widget can skip over entire words or to ends of lines rather than over single characters, as if the Option or Command keys were held; or select text as if Shift is held.
  • The keysym (%K) may also not correspond to the character to insert (%A). For example generating <Option-Shift-Key-O> inserts 'O' but has keysym Oslash. (I'm not sure what the expected behavior of this event should be.)
Another issue I'm not sure was introduced at any particular point: event generate $wid <Key-BackSpace> produces a %k value (0x33000008) different from actual backspace press (0x3300007f).


marc_culler (claiming to be Marc Culler) added on 2020-04-27 13:47:58:

Wow. Nice catch! Thank you!

I think this is fixed in 7a38ecf70a.


chrstphrchvz added on 2020-04-27 06:17:23:

As of [020b108beb] holding certain keys in Tk, then clicking away from Tk crashes due to dereferencing focusWinPtr == NULL at tkMacOSXKeyEvent.c:120.


marc_culler (claiming to be Marc Culler) added on 2020-04-26 03:08:45:
OK.  I think I squashed these last few glitches.

chrstphrchvz added on 2020-04-26 01:31:59:

It's '\u0010' that is getting inserted…

This also isn't due to any changes on bug-585584ad66; this behavior was already present in 8.6.10 and core-8-6-branch. I just expect pressing Menu to not insert anything (especially nonprinting zero-width characters).

Something that is broken on this branch: as of [dbc9845c9c] pressing modifier keys inserts NUL/'\0' characters (which also manifest as nonprinting zero-width characters).


chrstphrchvz added on 2020-04-25 23:04:05:

…actual keypresses of the newly-recognized Menu key insert a nonempty %A (I haven't yet determined exactly what: some mysterious zero-width character that doesn't get printed by puts and can't be copied out of the widget).

It's '\u0010' that is getting inserted, I think. (These can get copied out of Tk; they just can't be pasted into to too fancy of a text analyzer as I was using.)


chrstphrchvz added on 2020-04-25 22:46:25:

Brad, if I understand correctly, the important thing to extract from [382712ade6] is that Tk should just have %A for <KeyPress> and not <KeyRelease> regardless of platform, at least for some future Tk version. Is this correct?

I have not been paying close enough attention to all the ways %A behaves on Aqua (probably from focusing too much on the unbound function key nonempty %A insertion issue). Its behavior has changed recently and might no longer match documentation. In 8.5.9/8.6.10/core-8-6-branch prior to [14376ab5b9], for actual key events, %A was at least defined for both <KeyPress> and <KeyRelease>, regardless of widget type. In core-8-6-branch as of [14376ab5b9], for actual key events, %A is now only defined for text/entry widgets, and only for <KeyPress>; not at all for other widgets. Currently on bug-585584ad66 (there's a new check-in as I was writing…[dbc9845c9c]), %A has gone back to being defined for all widgets and for both <KeyPress> and <KeyRelease>.

The only thing I now notice being off so far is that actual keypresses of the newly-recognized Menu key insert a nonempty %A (I haven't yet determined exactly what: some mysterious zero-width character that doesn't get printed by puts and can't be copied out of the widget).


chrstphrchvz added on 2020-04-25 21:19:31:

Nevermind, there is something about how I was trying to automatically test event generate for unbound function keys with text/entry, e.g. event generate .entry <Key-F16>, that exhibits %A (ᅪ) being inserted only some proportion of the time. The issue never disappeared/reappeared; it began with [95c18e3b64] and is still the case for [020b108beb].


bll added on 2020-04-25 21:16:51:
Review 
   https://core.tcl-lang.org/tk/tktview?name=382712ade6
for information on %A .

chrstphrchvz added on 2020-04-25 20:49:03:

Actually, nonempty %A inserting into text/entry widgets for unbound function keys seems to have appeared/disappeared/reappeared a few times in recent bug-585584ad66 check-ins. I'm still trying to find exactly which check-ins exhibit this.


chrstphrchvz added on 2020-04-25 19:34:54:

More testing of the original issue reveals that while actual key events for function keys and various other non-inserting keys have the expected empty string ({}) as their %A substitution, as of [95c18e3b64] programmatically generated events for those keys have non-empty %A containing the character for the codepoint contained in %N. Examples:

%K       %N      %A
F1       0xffbe  ᄒ
Up       0xff52  r
Home     0xff50  p
Shift_L  0xffe1  £

This issue does not cause keys with special bindings for entry/text widgets to insert characters, but in part this is because function keys F1 and F5-F12 have a binding defined in the workaround for [0e6930dfe7] (which was caused by non-empty %A; once there are no more issues like this, I would like for that workaround to be reverted).


marc_culler (claiming to be Marc Culler) added on 2020-04-25 13:41:08:
But, on the other hand, the Text widget's calls to Tk_SetCaretPos are in the
same file where the crash occurs, and tkMacOSXKeyEvent.c uses the side effects
of that call.  So maybe it is related.

I made a change to how that works, on a hunch that it might be related to this
crash.  I no longer maintain a static pointer to the caret window.  This was
unnecessary anyway.

Nicolas, can you please test the latest commit and see if you can still produce
your crash?

nab added on 2020-04-25 13:40:21:
Hi,
this crash occurred by using tableList and I saw the log tells about Tk_DestroyWindow and I know how things can behave badly on Aqua, so I'll try to idle stuff.

1) I'm trying to make a script to reproduce the crash but no success yet
2)I'll try but it's a long process :)

here's the first part of the crashLog:
Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000010
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [86545]

I think your analyse is correct and I'll try to "delay" updates.

thanks,
++

marc_culler (claiming to be Marc Culler) added on 2020-04-25 12:51:34:
Another thing that makes me skeptical about whether this crash is related to
Key events is that the event being processed when the crash occurs is not a
key event.  It is either a FocusIn or FocusOut event (according to the
traceback).  It would really help to know what user actions are involved when
the crash occurs.

marc_culler (claiming to be Marc Culler) added on 2020-04-25 12:44:04:
There are two basic questions that you haven't answered.

(1) Do you have a reliable way to produce the crash? (If so, please at least
give a hint about what that is, what widgets are involved, what actions are
you making when the crash occurs.)
(2) Can you find an earlier commit where the crash does not occur?  Which one?

Also, I assume the crash is a segmentation fault, but that part of the crash
report is missing.  Is that correct?  The traceback indicates that the crash
occurs during destruction of a window, and that at the exact moment of the
crash a Text widget is attempting to mark some text.  I am guessing that the
Text widget is inside the window which is being destroyed.  But it is hard to
confirm any of this without more information from you.

nab added on 2020-04-25 09:31:44:
as this branch is far enough from the original ticket I thought I should post here...
but as you wish, if you prefer a new ticket I'll open a new one indicating the crash happens with this branch.

++

marc_culler (claiming to be Marc Culler) added on 2020-04-24 23:02:45:
On second thought, I do see a way that this could be related.  I'll look at it.
Thanks.

marc_culler (claiming to be Marc Culler) added on 2020-04-24 22:41:28:
Hi Nicolas,

I don't see any possibility that your crash is related to any of the changes
having to do with key events.  That crash occurred in generic code.  It looks
like a Text widget was trying to mark some text after its window had been closed.

If you can reproduce the crash in some reliable way it deserves its own ticket.

nab added on 2020-04-24 22:12:58:
Hi Marc,
thank you for your work...
using latest checkin of this branch, the issue is fixed for the script I've posted, but there is other cases where it behaves badly... but i do not succeed to make a short script.

I had a crash... here is it with the symbols:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   Tk                            	0x000000010931b47f TkTextMarkSegToIndex + 95 (tkTextMark.c:400)
1   Tk                            	0x00000001092f7362 TextEventProc + 802 (tkText.c:2545)
2   Tk                            	0x000000010923ff52 Tk_HandleEvent + 674 (tkEvent.c:1271)
3   Tk                            	0x0000000109240da9 WindowEventProc + 217 (tkEvent.c:1724)
4   Tcl                           	0x0000000109170f90 Tcl_ServiceEvent + 144
5   Tcl                           	0x00000001091711b2 Tcl_DoOneEvent + 162
6   Tk                            	0x00000001093835da TkWmDeadWindow + 2010 (tkMacOSXWm.c:1018)
7   Tk                            	0x000000010927175e Tk_DestroyWindow + 1358 (tkWindow.c:1426)
8   Tk                            	0x00000001092714de Tk_DestroyWindow + 718 (tkWindow.c:1335)
9   Tk                            	0x00000001092714de Tk_DestroyWindow + 718 (tkWindow.c:1335)
10  Tk                            	0x0000000109321f9e TkTextWinFreeClient + 110
11  Tk                            	0x000000010932040d EmbWinDeleteProc + 141 (tkTextWind.c:761)
12  Tk                            	0x0000000109303836 TkBTreeDeleteIndexRange + 1030 (tkTextBTree.c:1453)
13  Tk                            	0x00000001092f8d18 DeleteIndexRange + 1816 (tkText.c:3280)
14  Tk                            	0x00000001092f58b9 TextWidgetObjCmd + 5737 (tkText.c:1114)
15  Tcl                           	0x00000001090b79c1 0x109098000 + 129473
16  Tcl                           	0x00000001090b3a36 Tcl_EvalObjv + 342
17  Tcl                           	0x00000001090b536f 0x109098000 + 119663
18  Tcl                           	0x00000001090b485a Tcl_EvalEx + 26


++

marc_culler (claiming to be Marc Culler) added on 2020-04-24 17:59:23:
Indeed, it is related and is now fixed in the bugfix branch.

We now use the TextInputClient protocol only for key events that target the
current caret window.  Widgets that handle text input show a blinking caret
and they notify Tk of the location of their caret by calling Tk_SetCaretPos.
This is how we determine which window is the caret window.  But we should not
declare a window to be the caret window every time it draws a caret.  It
also needs to be drawn in the NSWindow which is the NSApplication's current
KeyWindow. Otherwise it will not receive key events at all.

When running Nicolas's script in the Wish app, the console window would become
the caret window every time that it drew its caret and the entry widget would
become the caret window every time it drew its caret.  So a key event for the
entry widget would only get sent to the TextInputClient if it happened that
the most recently drawn caret was the one in the entry widget.

Thanks again, Nicolas!

marc_culler (claiming to be Marc Culler) added on 2020-04-23 23:52:40:
I don't know if this is related or not, and since I can't reproduce it I don't
have much to go on.  Can you find the first commit where this problem appeared?

Could your keyboard be failing? Maybe you could try a different keyboard.

nab added on 2020-04-23 19:21:32:
well here the "Entries without scrollbars" demo does not display every letters I type.
settings of key repeat in preference display is set to middle, lang is french

should I open a new ticket?

++

marc_culler (claiming to be Marc Culler) added on 2020-04-23 16:49:03:
I edited the code for the "Entries without scrollbars" demo to make it use
ttk::entry instead of entry.  When I type 'a' 10 times as fast as I can I
see 10 'a' characters displayed.  I don't see any problems when I type 'a'
10 times in one of the entries in your script either. Also, the <Return>
bindings work now.  So I am not sure what to make of this.

You aren't triggering the key repeat are you?  That should cause a menu to
pop up with accented letters, and it will not respond to anything but number
keys to select the accent.

nab added on 2020-04-23 16:00:17:
Hi,
I've downloaded/compile your latest code of this branch.
when I click inside a ttk::entry and type 10 times the letter a, there's no 10 'a' displayed.

I'm posting here because of the <Return> binding that was not triggered each times (reported few days back) and because you mentioned TextInputClient...

++

marc_culler (claiming to be Marc Culler) added on 2020-04-23 15:45:25:
Nicolas, please explain how to see the bug with that script. Also, please test
against the latest commit to the bugfix branch.  And please explain why you
think this bug should be on this ticket instead of having its own ticket.
Is there a previous commit which did not have this problem?

nab added on 2020-04-23 13:08:41:
Hi,
there seems to be a lag in ttk::entry when you type text fast, not all letters are displayed
it happens with this branch as well as  the mac_styles branch.

using this script:
package require Tk
package require Ttk
proc displayStuff {} {
    for {set x 1} {$x<10} {incr x} {
        set this($x) [\
        ttk::entry .b$x\
        ]
        bind .b$x <Escape> {focus -force .}
        grid $this($x) -sticky news
    }
    grid columnconfigure . all -weight 1
    grid rowconfigure . all -weight 1
}
displayStuff
bind . <Control-Tab> {puts "Ctrl+Tab pressed"}

marc_culler (claiming to be Marc Culler) added on 2020-04-20 19:22:08:
Thank you for noticing my over-zealousness in relocating declarations!

I fixed the two typos and moved the initialization of grabWinPtr so that it
happens after checking that winPtr is non-NULL.  That was definitely a crash
waiting to happen.

nab added on 2020-04-20 19:03:13:
Hi Marc,
I've test your new code and all seems fine.
when ready would you mind merging it to mac_styles branch for more testing.

best regards,
++

chrstphrchvz added on 2020-04-20 19:02:52:

Marc, thanks for your work and patience with going back and forth on this code. I haven't yet tested [769f555de3], but here are my comments after reading:

  • tkMacOSXKeyboard.c: looks OK
  • tkMacOSXKeyEvent.c:

    There is a test for (!winPtr) at line 66, but winPtr is already assumed to be non-NULL at line 49. So if winPtr == NULL, rather than returning at line 67, wouldn't it crash first at line 49? Is the check redundant, or should dereferencing winPtr remain after it?

    line 83: stray '<' in comment

    line 128: preferred spelling would be 'targeting'


marc_culler (claiming to be Marc Culler) added on 2020-04-20 18:44:34:
Another bonus seems to be that dictation now works.  The first time that I
tried to dictate text into an entry widget nothing happened and clicking the
button to end dictation produced a "Wait" animation in the popup menu.  But
I think it just takes a while to load the speech processing library.  I made
a second attempt, and the dictation worked fine.

marc_culler (claiming to be Marc Culler) added on 2020-04-20 17:22:46:
Thank you Christopher!

I have incorproateded your suggestions, and fixed the issue of <Return> events
not being seen by text and entry widgets.  This is another major rewrite.  I
believe that the code is now correct and that the logic is now clear enough
that one can actually verify that it is correct by reading it.

A bonus from using the TextInputClient correctly is that the dead keys now
actually work in text and entry widgets to enter accented characters for which
composition sequences are defined.  The popup menu also works.

I will clearly need to add some non-regression tests since all tests were
passing with the previous commit even though it was seriously broken.

chrstphrchvz added on 2020-04-20 15:31:16:

I have not tested [7e861e1ec3] much, but I do observe similar issues as Nicolas: return and tab stopped working in text widget as of [a0030742e2].

Some comments for [7e861e1ec3]:

  • tkMacOSXKeyEvent.c:

    line 111: uninitialized xEvent is used—was this block supposed to go after setupXEvent()?

  • tkMacOSXKeyboard.c:

    line 80: {105, XK_F13, NSF13FunctionKey} is duplicated

    Other suggestions while we're here:

    line 105: modKeyArray[] can also be declared const

    line 122: can use BOOL instead of int for initialized and keyboardChanged

    Possible separate bug: I notice that keyboardChanged is unset in two places (in KeycodeToUnicode() at line 302, and in XKeysymToKeycode() at line 567). This is what Tk Cocoa has always done, but is it correct, or does it leave either latin1Table[] or KeycodeToUnicode()'s *uchr and keyboardType outdated after an NSTextInputContextKeyboardSelectionDidChangeNotification?


marc_culler (claiming to be Marc Culler) added on 2020-04-20 13:28:45:
Thank you Nicolas!  I am on it.  Another opportunity for more code clarity arises.  Look for a new commit later today.

nab added on 2020-04-20 08:54:17:
hi,
I don't know if it's related but using the following script with this branch, when you click inside the spinbox,  <Return> binding is not triggered each time, with the mac_styles branch <Return> is triggered each times.

package require Tk
package require Ttk

proc displayStuff {} {
    set x 1
    set this($x) [\
     ttk::label .b$x -text abcdefghijklmnopqrstuvwxyz\
     ]
    set that($x) [\
     ttk::spinbox .s$x  -from 1 -to 100 -increment 1 -width 3\
     ]

    bindtags $that($x) [linsert [bindtags $that($x)] 0 pad]

    grid $this($x) $that($x) -sticky news
    grid columnconfigure . all -weight 1
    grid rowconfigure . all -weight 1
}

displayStuff

bind pad <Return> {puts "that %W"}

marc_culler (claiming to be Marc Culler) added on 2020-04-19 23:52:10:
I committed another major revision to the bugfix branch.  I applied the patch,
I fixed the crash, I renamed variables and I added a const declaration.

But I also reworked the whole key handling process.  The problem that was first
reported here for function keys also applied to other keys, like Home, PageUp,
End, PageDown, Clear etc.  That is now fixed.  The previous code did not
distinguish betwee left and right shift, control and option keys.  Now it does.
The stub function XKeysymToKeycode was previously not used and not implemented
correctly.  But it should have been used - it is exactly what is needed to
generate events correctly.  The static function that was being used before is
now gone.  The hash tables have also been reworked, to make them do what was
needed for XkeysymToKeycode.  I removed the comment about replacing the hash
tables with arrays as I think that the hash tables make much more conceptual
sense and do not add much overhead.

As usual, testing is needed and much appreciated.

chrstphrchvz added on 2020-04-19 15:08:07:

I noticed that pressing dead keys crashes Tk as of a recent change in core-8-6-branch (see [8d8e9c8279]); that may be worth fixing as part of changes for this issue.


chrstphrchvz added on 2020-04-18 10:11:34:

I reuploaded the patch since the second savedModifiers = modifiers toward the end of the method similarly needs to only happen for NSFlagsChanged events.


chrstphrchvz added on 2020-04-18 03:39:37:

I observe that pressing Fn produces the ?? keycode when it is pressed immediately after pressing a function key (without the aid of Fn being held). I find that this is because function keys have their NSKeyDown events' NSFunctionKeyMask modifier flag set, even though they are not modifier keys. This causes the NSFunctionKeyMask flag to be set in savedModifiers without pressing Fn. If Fn is the next key to be pressed, a KeyPress event with a keycode of (modifiers ^ savedModifiers) → (NSFunctionKeyMask & NSFunctionKeyMask) → 0 is generated, which TkpGetKeySym() returns NoSymbol (?? keycode) for. I believe that, to fix this, savedModifiers should only be updated by NSFlagsChanged events; I have attached a patch with this change. I'm not sure this resolves the instances where Marc observed the ?? keycode for Fn.


chrstphrchvz added on 2020-04-18 01:47:17:

case NSKeyDown could be rewritten to fallthrough to case NSKeyUp

Sorry, I am completely wrong about this. They assign different values to xEvent.xany.type.


chrstphrchvz added on 2020-04-18 01:44:23:

[a0030742e2] looks good so far. Note that it implements or supersedes changes I proposed in [88e90b22c6], with the exception of adding {90, XK_F20} to keyArray[] (which I think should still be included for completeness).

I would avoid using unichar as a variable name, because that is a valid type name.

The comment above InitKeyMaps() suggests replacing it with plain arrays instead of using a Tcl hash table; would that be something to try doing now?

I suggest keyArray[] (and possibly other arrays) be also declared as const, since there's no need to modify them at runtime.

case NSKeyDown could be rewritten to fallthrough to case NSKeyUp (unless not using break is confusing):

	case NSKeyDown:
	    if ([charactersIgnoringModifiers length] > 0) {
		has_modifiers = xEvent.xkey.state &
		    (ControlMask | Mod1Mask | Mod3Mask | Mod4Mask);
	    }
	case NSKeyUp:
	    xEvent.xany.type = KeyRelease;
	    xEvent.xkey.keycode = (keyCode << 16) | unichar;
	    break;


marc_culler (claiming to be Marc Culler) added on 2020-04-17 18:35:43:
I think this is fixed in commit a0030742e2 to the bug-585584ad66 branch.
Basically it was an inconsistency in how XEvent keycodes are handled.  The
branch does some more cleanup to the code in tkMacOSXKeyEvent.c as well as
cleaning up tkMacOSXKeyboard.c.

Please test!

chrstphrchvz added on 2020-04-15 22:50:40:

The Super_L keysym appears to correspond to the Fn key by itself. I can almost always get the F1 keysym to appear when I expect it, unless Fn is pressed rapidly enough that I might end up with a ?? keysym, usually after Tk being unresponsive for a few seconds; I think it may be trying to enter dictation (since pressing Fn twice is the default shortcut for it), which doesn't work reliably—see [fdbdcc8c3c].

Marc, do you happen to have any key remapping utility like Karabiner-Elements present? I don't suspect it to be an issue, but just in case. I had used Karabiner-Elements in the past to generate key/button events, which was quite useful for testing, though I may have also encountered some strange issues with it as well—it's been too long to remember exactly whether that was not the case.

When I had last looked into this issue several months ago, I had the impression that generating events and receiving/handling events from input were almost completely separate code paths that lacked reuse or synchronization. I wouldn't have initially considered issues with receiving events as part of this ticket's issue, but I'm interested in hearing about issues receiving events.


marc_culler (claiming to be Marc Culler) added on 2020-04-15 22:14:17:
When I have that option selected I cannot bind to <Key-F1>.  If I turn it off
and use the fn key, then I can bind to <Key-F1> and I get the keycode ?? except
sometimes I get Super_L as the keycode.

There is definitely something broken here.

chrstphrchvz added on 2020-04-15 20:57:52:

I am not able to bind to <Key-F1> at all. Neither keypress events nor generated events trigger the handler…Am I missing something?

I am able to bind to <Key-F1> etc., but I have to hold Fn since I don't have "Use F1, F2, etc. keys as standard function keys" enabled in System Preferences > Keyboard.


marc_culler (claiming to be Marc Culler) added on 2020-04-15 20:21:42:
Is this really an issue with event generate?  I am not able to bind to <Key-F1>
at all.  Neither keypress events nor generated events trigger the handler.
Am I missing something?

chrstphrchvz added on 2020-04-14 02:08:44:

A couple of use cases that have since occurred to me:

  • "Macros" for automating repetitive input or for controlling a program via a script (though event generate is still not compulsory; programs could offer some more friendly way of controlling them with a script, and plenty of third-party tools exist for automating/simulating user input to a program, e.g. AutoHotKey on Windows, Automator/AppleScript on Mac, xdotool on X11).
  • Simulating input for guided tutorials (combined with pointer warping; this use case might no longer be a good idea in general).


chrstphrchvz added on 2019-08-21 17:13:40:

Can you help me to understand the use case here?

Indeed I don't know if this sort of use is strictly necessary for normal practical programs. So I'm not sure I can think of a good example for wanting this functionality to work other than for testing purposes (debugging or unit tests), and/or just the expectation of it being available and behaving as it does on other platforms.


kevin_walzer added on 2019-08-21 12:45:00:

Can you help me to understand the use case here? I see what you are referring to here, but it only applies if you are trying to capture the exact keypress with the event. If the keypress event is being bound to some command, then it fires without issue, at least after the latest commit.

pack [text .t]; bind all <KeyPress> {event generate . <<press>>} bind all <<press>> {.t insert end hello!\n}

This snippet inserts "hello" in a text widget, without any of the other garbage.


kevin_walzer added on 2019-08-21 12:42:45:

Can you help me to understand the use case here? I see what you are referring to here, but it only applies if you are trying to capture the exact keypress with the event. If the keypress event is being bound to some command, then it fires without issue, at least after the latest commit.

pack [text .t]; bind all <KeyPress> {event generate . <<press>>} bind all <<press>> {.t insert end hello!\n}

This snippet inserts "hello" in a text widget, without any of the other garbage.


Attachments: