Tk Source Code

View Ticket
Login
Ticket UUID: 131adc6b77dc65d55edc0d108566226079976cb7
Title: bound buttonpress is passed on to other program
Type: Bug Version: 8.6.18, 9.1.0
Submitter: anonymous Created on: 2016-03-01 16:06:29
Subsystem: 01. Bindings Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-11-10 17:30:45
Resolution: Fixed Closed By: nemethi
    Closed on: 2026-11-10 17:30:45
Description:
Start the "trap" program below, then start the test program, position
the test window over the trap window.  Then click a mouse button. 

This can also be tested by simply positioning the test window over the
windows desktop and using a right-click.  Windows will open a context
sensitive menu.

The mouse buttonpress is not consumed and is passed on to the other program below it.

An action performed in one program should never be left unconsumed and 
should not be passed on to the program below it.

Reference: https://groups.google.com/d/topic/comp.lang.tcl/bVtTlGgdU-U/discussion

A trap program:

#!/usr/bin/tclsh
package require Tk

proc doexit {} {
  update
  exit
}

bind . <1> [list . configure -bg yellow]
bind . <2> [list . configure -bg cyan]
bind . <3> [list . configure -bg green]
. configure -width 300
. configure -height 300

And this is the test program:

#!/usr/bin/tclsh
package require Tk

bind . <1> exit
bind . <2> exit
bind . <3> exit
User Comments: nemethi (claiming to be Csaba Nemethi) added on 2026-11-10 17:30:45:

Serhiy, many thanks! Merged into main, core-9-0-branch, and core-8-6-branch by commits [d1af9365], [5f5bd4fe], and [f84582b4].


serhiy.storchaka added on 2026-10-23 19:17:33:

Confirmed on Windows 11 (8.6.18, 9.1): when a binding on the ButtonPress destroys the window, the ButtonRelease goes to the window below. Tk had released its mouse grab when the window died, so it sent the remaining button and motion events to the window then under the pointer. X11 is not affected.

Fixed on branch pointer-dead-restrict-window ([27801b131f]): these events are discarded until all buttons are released, so other Tk windows of the application no longer get them. Tested on Windows and Linux.

This cannot stop the release reaching another application: Windows cancels the mouse capture when the window is destroyed, and if the application exits on the press, nothing is left to catch the release. bind.n now recommends binding such actions to ButtonRelease.


bll added on 2018-07-21 14:03:32:
Similar to the EatSpuriousMessageBugFix() function, the following could
be added to the windows code immediately before the window is destroyed.

    while (PeekMessage(out msg, IntPtr.Zero,
        WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
        ;

bll added on 2018-07-21 13:33:40:
Still extant on windows.
No leak on Linux XFCE.

(Updated version)

anonymous (claiming to be [email protected]) added on 2016-10-11 15:02:10:
Updated title as this is not windows specific.

This is a general problem where mouse events in one toplevel window are being leaked to the window below.

On Windows, this is particularly bad as the mouse events are leaked through to the OS or other application.

Another reference:

http://stackoverflow.com/questions/39970281/mouse-events-duplicated-with-tk-menu-commands-not-accelerator-keys

anonymous (claiming to be [email protected]) added on 2016-08-04 18:39:58:
Linux 8.6.1 also: Clicking on a selection in a top-level window (the bind is: <Double-ButtonPress-1>) and the click is passed through to the (tk) window below.

anonymous (claiming to be [email protected]) added on 2016-08-04 18:20:46:
This problem is extant with version 8.6.6 on Windows.

I have a top-level window where I double click on a selection (the bind is: <Double-ButtonPress-1>) and the click is passed through to the (tk) window below.

Updating version and changing priority to important.