Tk Source Code

View Ticket
Login
Ticket UUID: 222027
Title: "event generate .myentry <KeyPress-bracketleft>" fails
Type: Bug Version: obsolete: 8.3.2
Submitter: nobody Created on: 2000-11-09 07:00:40
Subsystem: 69. Events Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2016-08-06 17:07:09
Resolution: Invalid Closed By: fvogel
    Closed on: 2016-08-06 17:07:09
Description:
Some of the special keys don't work with event generate, e.g., backslash, brackets, braces, @, $ etc.

Take a look at this code:

     console show
     entry .e
     pack .e
     focus -force .e
     update idle

     bind .e <KeyPress> {puts "keypress %K %k"}

     after 1000 {
         event generate .e <KeyPress-A>
         event generate .e <KeyRelease-A>
         event generate .e <KeyPress-bracketleft>
         event generate .e <KeyRelease-bracketleft>
         event generate .e <KeyPress-B>
         event generate .e <KeyRelease-B>
         event generate .e <KeyPress-greater>
         event generate .e <KeyRelease-greater>
         event generate .e <KeyPress-C>
         event generate .e <KeyRelease-C>
     }

It produces "AB>C" - no bracketleft.

Platform:
   tcl_platform(byteOrder) = littleEndian
   tcl_platform(machine)   = intel
   tcl_platform(os)        = Windows NT
   tcl_platform(osVersion) = 5.0
   tcl_platform(platform)  = windows
   tcl_patchLevel: 8.3.2

See also thread on comp.lang.tcl on 2 November 2000.
User Comments: fvogel added on 2016-08-06 17:07:09:
In a nutshell: when generating the event for the <bracketleft> keysym, Tk creates the event according to the user's platform keyboard layout. This means that the keycode and state corresponding to a given keysym may be different for different keyboard layouts. And this is perfectly correct. The documentation for "event generate" tells it (cryptically: no implications are mentioned): it generates a window event and *"arranges for it to be processed just as if it had come from the window system"*.
So at event processing time the event processing code receive an event which content (keycode and state) depends on the keyboard layout of the platform/system that happen to run the "event generate" code.

Then, on some platforms event generating <bracketleft> results in an event that is not the same as from a simple [ key stroke: there may be added modifiers. In such case, the entry widget (and this is the same for other widgets) does not display the [ because the binding matching code triggers a more specific binding than <KeyPress>, which is <Alt-KeyPress> (on some platforms at least), and this is bound to {# nothing}, which is definitely correct as per the comments about this in entry.tcl.

Conclusion: what is reported is in fact not a bug.

fvogel added on 2016-08-05 13:10:49:
I had a look regarding the case of (the differences between):

    event generate .e <KeyPress-f>  ; # which works for me
and
    event generate .e <KeyPress-bracketleft>  ; # which doesn't work for me


My findings are:

  - Event description parsing, and filling of the event.general structure is OK in HandleEventGenerate() for both. With <f> we have state=0 and keycode=70, and with bracketleft we have state=20 and keycode=73 in the event.general.xkey structure. All this is correct considering the layout of my keyboard (20 corresponds to Control+Mod2 modifiers, which are in fact a single "AltGr" key on my keyboard).

  - Since we didn't speecify asynchronous events, HandleEventGenerate() calls Tk_HandleEvent() immediately with the constructed event. With <f> the entry widget functions are called (EntryWidgetObjCmd -> InsertChars and so on), with <bracketleft> they are just not.

  - In Tk_BindEvent, MatchPatterns() does not return the same thing for both sequences. It tourns out that with <f> the Tcl code bound to "bind Entry <KeyPress>" in library/entry.tcl is called, but this does not happen for <bracketleft>.

  - And the reason is that for <bracketleft> a more specific binding was matched in MatchPatterns():
    bind Entry <Alt-KeyPress> {# nothing}
    bind Entry <Control-KeyPress> {# nothing}
(library/entry.tcl)

So the behavior indeed depends on the keyboard layout of the user.

For some users, due to their specific keyboard layout, typing [ does not need a modifier and the <KeyPress> entry class binding triggers. The bracket is shown in the entry box.

For other users with other keyboard layouts however, this does not happen because a more specific binding is matched instead, and this binding does nothing.


Changing the above class bindings to:
    bind Entry <Alt-KeyPress> {}
    bind Entry <Control-KeyPress> {}
makes it work then: the left bracket shows up in the entry box nicely.

But then of course doing this breaks the case for which these {# nothing} scripts were put in the place, i.e. (entry.tcl:198-201)

# Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
# Otherwise, if a widget binding for one of these is defined, the
# <KeyPress> class binding will also fire and insert the character,
# which is wrong.  Ditto for Escape, Return, and Tab.

fvogel added on 2016-07-29 20:55:53:

Probable duplicate is [e36963bfe8].


anonymous added on 2016-01-23 01:08:20:
Under linux/X11, with 8.6.4, actually tcl/tk trunk around submit time I get 

A8B>C

which also looks just plain wrong, at first look, just in another way.

Terminal output is:

keypress A 38
keypress 8 17
keypress B 56
keypress greater 94
keypress C 54

tcl_platform(byteOrder)     = littleEndian
tcl_platform(machine)       = x86_64
tcl_platform(os)            = Linux
tcl_platform(osVersion)     = 3.4.63-2.44-desktop
tcl_platform(platform)      = unix
tcl_platform(pointerSize)   = 8
tcl_platform(threaded)      = 1
tcl_platform(wordSize)      = 8

fvogel added on 2015-10-27 22:22:32:

This problem also bit in [1669632].


fvogel added on 2015-10-27 22:14:52:
I can see this problem (thus re-opening the ticket).


1. On Windows Vista, with core-8-5-branch and trunk:

The entry widget shows "AB>C" whereas it should show "A[B>C".

The console shows the bindings fire:

(bin) 7 % 
after#1
keypress A 65
keypress bracketleft 53
keypress B 66
keypress greater 226
keypress C 67
(bin) 8 % 

The keysym (%K) is correct.


2. On Linux Debian 6.0, with core-8-5-branch and trunk:

The entry widget shows "A(B>C" whereas it should show "A[B>C".

The console shows the bindings fire:

% after#0
% keypress A 24
keypress parenleft 14
keypress B 56
keypress greater 94
keypress C 54

The keysym (%K) is not correct: bracketleft became parenleft


Also, on both systems the keycode (%k) are not consistent with the keysyms man page: http://www.tcl.tk/man/tcl8.5/TkCmd/keysyms.html

On Linux at least this is probably due to the specific modmap I use.


The bottom line is that:

  event generate .e <KeyPress-bracketleft>
  event generate .e <KeyPress-backslash>
  ... (and certainly a few others) ...

do not actually trigger what you may think it fires.

hobbs added on 2000-11-17 07:23:44:
This works just fine for me on Win2k with 8.4a2.

ericm added on 2000-11-10 00:35:47:
I am unable to reproduce this on NT 4, with Tcl/Tk 8.3.2 or 8.4a2.  This may be a Windows 2000 specific error, but I don't have the equipment to verify that.

Eric Melski
The Other Tcl Guy
Interwoven, Inc.
ericm at interwoven.com