Tk Source Code

View Ticket
Login
Ticket UUID: f492c3de04c26a18cfe9c116c1f67ff3b8f3cbae
Title: MS-Win: characters entered via an IME or the touch keyboard get the keysym of a function key (F1, F3, F5, ...)
Type: Bug Version: 8.6, 9.0, main
Submitter: serhiy.storchaka Created on: 2026-09-18 17:30:25
Subsystem: 69. Events Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-09-23 08:33:29
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-09-22 19:06:22
Description:

On Windows, text committed by an input method (e.g. Microsoft Pinyin: type "r", press Enter) or entered with the touch keyboard produces a KeyPress event whose keysym is a function key:

character   %A   %K   %k
r           r    F3   114
t           t    F5   116
p           p    F1   112

so bindings to <F3>, <F5>, <F1> fire when such text is entered. In IDLE entering "r" through the Chinese IME opens the "Find Selection" dialog.

Cause: such text arrives as WM_KEYDOWN with wParam == VK_PACKET and the character in HIWORD(lParam). Since [aaa031e151] (ticket [6c0d7aec67]) TkWinChildProc() in win/tkWinX.c re-posts it as WM_KEYDOWN with the character as wParam, so the KeyPress event gets keycode == 'r' == 0x72, and TkpGetKeySym() interprets it as the virtual key VK_F3. The same happens for every character whose code is a virtual key code, i.e. most Latin letters and digits.

The re-post is not needed to get the character: TranslateMessage() already produces a WM_CHAR for the VK_PACKET message. Such characters should be handled like other WM_CHAR messages without a preceding key (keycode 0), with the keysym derived from the character rather than from a virtual key.

Reported against Tkinter: https://github.com/python/cpython/issues/125349, https://github.com/python/cpython/issues/146030

User Comments: serhiy.storchaka added on 2026-09-23 08:33:29:

Merged into core-8-6-branch by commit [4a7d6f6b76].


jan.nijtmans added on 2026-09-22 19:06:22:

Fixed [a444f9c1a5359913|here], also backported to Tk 9.0


jan.nijtmans added on 2026-09-22 07:11:12:

I'm having a look


serhiy.storchaka added on 2026-09-22 06:29:42:

Added test event-8.4 in [0ab232eaf6]: characters entered via an input method (Latin, Cyrillic and a non-BMP character which arrives as a surrogate pair) generate only the KeyPress/KeyRelease pairs with %A set; before the fix there was an extra pair with empty %A for each VK_PACKET keystroke. The test uses the testsendinput command (testsendinput unicode sends the characters with KEYEVENTF_UNICODE), which came with the merge of the win-unicode-keyinput branch ([837a9f1dcc]), since this branch is based on it.

The same command is added on the branches for [52e9b0f52c] and [2225507]. Merging them will conflict in win/tkWinTest.c and tests/constraints.tcl: keep one copy of the command and the constraint (they are identical), and keep the tests of all branches.


serhiy.storchaka added on 2026-09-18 17:48:40:

Reproduced on Windows 11 by posting the message sequence of an IME (WM_KEYDOWN with VK_PACKET, WM_CHAR, WM_KEYUP): %K F3 %A "" %k 114 followed by the character event. Fixed on branch win-vk-packet in [f173717bf1] (on top of win-unicode-keyinput): no events are generated for the VK_PACKET key itself, the WM_CHAR message generates the key events with keycode 0.