Tk Source Code

View Ticket
Login
Ticket UUID: 86299fe7ced4db7e51fc12e8d3a1fda8448a59a7
Title: MS Windows: generation of crossing events upon a pointer warp is delayed up to 250 ms
Type: Bug Version: 9.1a1
Submitter: erikleunissen Created on: 2026-04-12 10:53:20
Subsystem: 69. Events Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2026-11-21 16:21:59
Resolution: None Closed By: nobody
    Closed on: 2026-04-14 10:16:11
Description:
On MS Windows, Tk uses a specific mechanism (implemented by TkWinPointerEvent)
that monitors changes to the screen pointer's position at fixed time intervals
of 250 ms. This mechanism generates window crossing events if needed.

While this automatic tracking mechanism seems to work OK for detecting pointer
motions induced by a user handling the pointer-device, a different situation
presents itself when the pointer is warped programmatically; a technique not
expected to be used in regular Tk-applications, but expected to be used in
tests.

Currently, the windows-specific implemenation of TkpWarpPointer just warps the
pointer. The generation of any window crossing events is left as a task for the
TkWinPointerEvent mechanism. This contrasts with other platforms, where the
platform-specific implementations of TkpWarpPointer call Tk_UpdatePointer
themselves immediately upon pointer warp.

This leads to an anomalous case of timing on MS Windows: crossing events after
a pointer warp will not be serviced until the TkWinPointerEvent mechanism detects
the change of the screen pointer's position. This leads to delays of up to 250 ms,
and surprises to test authors who are not aware of this situation, as witnessed
here: [c3f5c85322].

The present ticket intends to align the situation on MS Windows with that
other platforms, and let crossing events be generated immediately upon pointer
warp.
User Comments: erikleunissen added on 2026-11-21 16:21:59:
Thanks for testing.
Beats me.

serhiy.storchaka added on 2026-11-21 06:45:09:

Erik, I tested your change on Windows 11 in a VirtualBox VM (trunk, MSVC build), at 100% display scale. The test was run alone, 50 times for each variant, each time in a new process:

  • original test: 10 failures out of 50;
  • controlPointerWarpTiming (50 ms) after each warp: 17 failures out of 50;
  • controlPointerWarpTiming 100 after each warp: 13 failures out of 50.

All failures give |<Leave> .<Enter> .||, there were no setup failures. So waiting after the warp does not help; it seems that the delay is not caused by SendInput() returning before the pointer has moved.


erikleunissen added on 2026-11-20 22:25:37:
And here again with additional explanation and typo fix:


--- tests/event.test
+++ tests/event.test
@@ -1527,17 +1527,19 @@
     bind all <Leave> {append result "<Leave> %W"}
     bind all <Enter> {append result "<Enter> %W"}
     set result "|"
 } -body {
     # Let the screen pointer perform a back and forth warp in quick succession,
-    # while immediatly providing opportunity for the event loop to service any
+    # while immediately providing opportunity for the event loop to service any
     # crossing events.
     event generate . <Motion> -warp 1 -x -50 -y 50
-    update
+    controlPointerWarpTiming; # enforce pointer warp
+    update; # let <Leave> event be serviced
     append result "|"
     event generate . <Motion> -warp 1 -x 50 -y 50
-    update
+    controlPointerWarpTiming; # enforce pointer warp
+    update; # let <Enter> event be serviced
     append result "|"
     # On MS Windows with bug present:
     # The TkWinPointerEvent mechanism doesn't detect the previous changes to
     # the position of the screen pointer because the back and forth motion
     # happens entirely within one MOUSE_TIMER_INTERVAL. The value for the

erikleunissen added on 2026-11-20 21:59:26:
Serhiy,

I have an idea what could be the reason for the failures that you observe.
The corresponding fix is appended. But I don't have access to an environment
such as you mention. So I can't test it. Can you?

Erik.
--

--- tests/event.test
+++ tests/event.test
@@ -1530,13 +1530,15 @@
 } -body {
     # Let the screen pointer perform a back and forth warp in quick succession,
     # while immediatly providing opportunity for the event loop to service any
     # crossing events.
     event generate . <Motion> -warp 1 -x -50 -y 50
+    controlPointerWarpTiming
     update
     append result "|"
     event generate . <Motion> -warp 1 -x 50 -y 50
+    controlPointerWarpTiming
     update
     append result "|"
     # On MS Windows with bug present:
     # The TkWinPointerEvent mechanism doesn't detect the previous changes to
     # the position of the screen pointer because the back and forth motion

serhiy.storchaka added on 2026-11-20 12:15:00:

Test event-86299fe7ce is unreliable on Windows 11 in a VirtualBox VM (main, MSVC build). Running it alone 20 times, each time in a new process, it failed 11 times at 100% display scale and 5 times at 200%. In full test suite runs it fails occasionally. Every failure gives

|<Leave> .<Enter> .||
instead of |<Leave> .|<Enter> .|: both crossing events are only generated after the second update, as before the fix.


erikleunissen added on 2026-04-14 10:16:11:
* Github-CI is still OK after my latest changes (as expected)
* Merged this branch into trunk and core-9-0-branch
* Closing this branch now.

erikleunissen added on 2026-04-13 16:39:57:
OK. I'm going to apply a few more small improvements, and will merge if
Github-CI is still satisfied.

oehhar added on 2026-04-13 07:48:19:

Reason and fix looks good. You may merge, if you like.

Give a sign, if you recommend special testing.

Thanks for all, Harald


erikleunissen added on 2026-04-12 15:01:40:
Created a bugfix branch bug-86299fe7ce with non-regression test [79d4dc342e] and
proposed fix, imported from branch bug-c3f5c85322 [d59811a5d1].