Tk Source Code

View Ticket
Login
2026-09-23
08:33 • Ticket [a9bd2ab9] Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes status still Closed with 3 other changes artifact: 74ec9f8f user: serhiy.storchaka
08:04
Fix [a9bd2ab98b], [f492c3de04]: keyboard input on Windows

Backport of the fixes from 9.0 and 9.1.

A WM_KEYDOWN message with VK_PACKET, sent for a character entered via an input method or the touch keyboard, was re-posted with the character as the virtual key code, so the KeyPress event had the keysym of a function key. Now no events are generated for the VK_PACKET key itself; the WM_CHAR message produced by TranslateMessage() generates the key events.

GetTranslatedKey() retrieved the WM_CHAR messages with GetMessageA(), so Windows converted the characters to the code page of the keyboard layout, and TkpGetString() decoded them with a code page guessed from the input language. When the two differed, the typed characters were mangled. Now the messages are retrieved as Unicode and the characters are stored as UTF-8 in the key event, and the code page machinery is removed.

Tests event-8.4, event-8.5 and event-8.6, and the missing testsendinput constraint. check-in: 4a7d6f6b user: serhiy.storchaka tags: core-8-6-branch

2026-09-22
14:52 • Closed ticket [70484209]: Incorrect reproduction of the Azerbaijani letter "əƏ" in the Enter field. plus 7 other changes artifact: c0899f3c user: jan.nijtmans
14:52 • Closed ticket [51436f3e]: Certain characters (like ț and ș) become question marks in Entry Box on Windows plus 7 other changes artifact: d42e3361 user: jan.nijtmans
14:48 • Closed ticket [62f1343a]: Tk textbox not working with "Bengali" set as keyboard input language plus 7 other changes artifact: 4ed31115 user: jan.nijtmans
14:43 • Closed ticket [a9bd2ab9]: Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes plus 7 other changes artifact: 830fdf7f user: jan.nijtmans
10:25
Fix [a9bd2ab98b], [f492c3de04] ...: Keyboard input on Windows IME-related problems check-in: 73b1db6d user: jan.nijtmans tags: core-9-0-branch
10:12
Fix [a9bd2ab98b], [f492c3de04] ...: Keyboard input on Windows IME-related problems. check-in: a444f9c1 user: jan.nijtmans tags: trunk, main
07:12 • Ticket [a9bd2ab9] Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes status still Open with 4 other changes artifact: 891f9abc user: jan.nijtmans
06:29 • Ticket [2225507f] Win32 menu keyboard traversal broken status still Open with 3 other changes artifact: 27a5e233 user: serhiy.storchaka
06:29 • Ticket [a9bd2ab9] Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes status still Open with 3 other changes artifact: ca8f1c25 user: serhiy.storchaka
06:29 • Ticket [52e9b0f5] keysyms for non-ascii bound keys are missing status still Open with 3 other changes artifact: 6df518aa user: serhiy.storchaka
06:08
Add tests for [a9bd2ab98b], with a testsendinput command which injects keyboard input with SendInput() and switches the keyboard layout closed check-in: f904b5db user: serhiy.storchaka tags: win-unicode-keyinput
2026-09-17
20:34 • Ticket [a9bd2ab9] Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes status still Open with 3 other changes artifact: 3c474b4a user: serhiy.storchaka
20:26 • Ticket [a9bd2ab9]: 3 changes artifact: 651b3ec3 user: serhiy.storchaka
20:24
Fix [a9bd2ab98b]: Keyboard input on Windows depends on the input language rather than on the keyboard layout

GetTranslatedKey() retrieved the WM_CHAR messages of a Unicode window with GetMessageA(), so Windows converted the characters to the code page of the keyboard layout, and TkpGetString() decoded them with a code page guessed from the input language (at startup) or from WM_INPUTLANGCHANGE. When the two differ, e.g. an English input language with a Hungarian or Ukrainian layout, the typed characters were mangled (o with double acute became o with tilde) until the input language was switched.

Now the WM_CHAR messages are retrieved as Unicode and the characters are stored as UTF-8 in the key event, for WM_KEYDOWN as well as for WM_CHAR messages, and the code page machinery is removed. check-in: 04cfb8ed user: serhiy.storchaka tags: win-unicode-keyinput

2017-11-19
16:55 • Ticket [a9bd2ab9] Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes status still Open with 4 other changes artifact: 3462ff29 user: bll
15:08 • Ticket [a9bd2ab9]: 3 changes artifact: 979e5db5 user: dkf
15:07 • Ticket [a9bd2ab9]: 3 changes artifact: 09ae1756 user: dkf
08:56 • Ticket [a9bd2ab9]: 3 changes artifact: b17a5281 user: dkf
2017-11-17
06:58 • Ticket [a9bd2ab9]: 3 changes artifact: 01d481e8 user: bll
2017-11-16
15:17 • Ticket [a9bd2ab9]: 3 changes artifact: d5bfd8aa user: bll
15:16 • Ticket [a9bd2ab9]: 4 changes artifact: 2ea08ddb user: bll
2017-11-15
20:57 • Ticket [a9bd2ab9]: 3 changes artifact: 99e4fd18 user: fvogel
08:33 • Ticket [a9bd2ab9]: 4 changes artifact: 31b4f7f8 user: oehhar
06:55 • Add attachment 1Language_with2keyboardlayouts.png to ticket [a9bd2ab9] artifact: f43da02a user: fvogel
2017-11-14
22:17 • Ticket [a9bd2ab9] Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes status still Open with 3 other changes artifact: a6f45d3a user: fvogel
2017-11-13
08:36 • Ticket [a9bd2ab9]: 3 changes artifact: 9b6d3b65 user: dkf
2017-11-12
16:52 • New ticket [a9bd2ab9]. artifact: 4501619a user: dkf

Ticket UUID: a9bd2ab98bdedf4420f6f663efabf4448f88a614
Title: Partial WM_INPUTLANGCHANGE support ignores keyboard layout changes
Type: Bug Version:
Submitter: dkf Created on: 2017-11-12 16:52:08
Subsystem: 68. Win Window Operations 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 14:43:13
Description:

As reported here, Tk is not correctly responding to keyboard layout changes on Windows. It does respond to language changes, but ignores keyboard changes because it ignores the lParam that describes what the current keyboard layout is. (Programmers in non-English locales often switch keyboard layouts without changing overall languages.)

The correct behaviour is described here, but I don't quite see how that all ties in (I'm no expert in matters relating to the Windows API).

(bll) Edit: adding text from original stackoverflow question:

I have a Tkinter Entry widget and a Hungarian keyboard. When I press the ő button on the keyboard, the Entry widget displays õ. ű becomes û. (I haven't had this problem in other applications since Windows 3.1.)

I do have two language settings on this computer and when I start changing them around, the problem disappears.

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 14:43:13:

[a444f9c1a5359913|fixed] now in trunk and in core-9-0-branch.


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

I'm having a look


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

Added tests in [f904b5dbfc]: event-8.5 (Ukrainian layout, the Q key gives U+0439) and event-8.6 (Georgian layout, which has no ANSI code page: the Q key gives U+10E6, which was "?" before the fix). They use a new test command testsendinput (win/tkWinTest.c) which injects keyboard input with SendInput() and loads/activates a keyboard layout by its identifier.

The same command is added on the branches for [52e9b0f52c], [f492c3de04] 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 (their numbers do not overlap).


serhiy.storchaka added on 2026-09-17 20:34:05:

The same fix also resolves [62f1343ad2] (Bengali), [51436f3e4a] (Romanian Standard: ș, ț) and [7048420903] (Azerbaijani: ə), where characters outside the ANSI code page of the keyboard layout arrived as ?.


serhiy.storchaka added on 2026-09-17 20:26:44:

Reproduced on Windows 11 with Tk 9.1 and an English input language with a Hungarian keyboard layout (and likewise with a Ukrainian layout, where every letter is mangled). The cause is in GetTranslatedKey() (win/tkWinX.c): the WM_CHAR messages of Tk's Unicode windows were read with GetMessageA(), so Windows converted the characters to the code page of the keyboard layout, and TkpGetString() decoded them with a code page guessed from the input language — at startup from the language ID of the initial layout, later from WM_INPUTLANGCHANGE. When language and layout differ, the guess is wrong until the input language is switched, as observed.

Fixed on branch win-unicode-keyinput in [04cfb8ed11]: the characters are kept as Unicode (UTF-8 in the key event) and the code page machinery is removed.


bll added on 2017-11-19 16:55:17:
The issue described by the OP was that the mapping was initially incorrect.  Switching languages resolved the issue.  There is (or should be) no keyboard layout change as the user is switching languages while using the same keyboard.

dkf added on 2017-11-19 15:07:03:

It seems to me like an issue to do with when the mapping from keycodes to characters is loaded, as if it was set up once and is never altered when the user changes the keyboard layout. Why that matters in this case isn't clear to me, but even just from reading the comments in the Tk sources one can see that there's a lot of different (and bizarre!) ways that these can work; we're not doing something right, even if we usually manage to get away with it. (Also, this is a pretty obscure part of the Windows API; it's not something that most people have experience with debugging and it isn't very well described in the API docs.)

My very rough guess is that we're looking at one of the less common reconfiguration setups, but as an insight it doesn't really help with identifying a fix. Though the MS page on the message in question does say:

You should make any application-specific settings and pass the message to the DefWindowProc function, which passes the message to all first-level child windows.
I notice that we don't do that; perhaps we should?


dkf added on 2017-11-19 08:56:39:

The bug can't be Tkinter only, as the handling of these notifications is not part of the scripted layer. (It's the sort of thing that can only be done right from the C code.) It does require a specific setup to see, however: two very different keyboard layouts but where each is associated with the same (human) language by the OS.

As far as I can see, the issue is that in TkWinChildProc, the handling of WM_INPUTLANGCHANGE (i.e., calling UpdateInputLanguage, which updates input character encoding only, a formally separate thing) doesn't consider a keyboard layout change. It's different on X11, where a change of keyboard layout has its own message. I can see that we don't really do much with keyboard layouts at all (the only keyboard-related API we call is GetKeyboardLayout(0) during TkWinXInit) and I'm not at all surprised that there are problems.


bll added on 2017-11-17 06:58:38:
Since the OP is not updating the ticket, I suppose we could ask on comp.lang.tcl if anyone has a hungarian keyboard (not a US keyboard with a hungarian setting).

bll added on 2017-11-16 15:16:18:
Unable to reproduce:

Tcl/Tk 8.6.7
Windows 7, set as german keyboard switching between languages (EN, DE) (single keyboard, two languages).

It appears from the image provided that the user is on Windows 8 or 10 and he states that the problem _disappears_ when he switches languages.  So I think we need some details on what the initial condition is when it fails.

Does it only fail with a hungarian keyboard?  Does it fail after a reboot?  Or after some other application is run?

It also might be useful to figure out what type of keyboard returns the o-tilde and u-hat characters for those key positions on the hungarian keyboard.

Windows-7: The windows keyboard layout does not match the link provided in the stackoverflow question (because I have a US keyboard?).

fvogel added on 2017-11-15 20:57:58:
I have installed Python specially to anlyze this bug report. I can't reproduce. I think we lack details about what the OP at stackoverflow is doing. If no more details can be provided I'll close as invalid.

oehhar added on 2017-11-15 08:33:36:

I think, the Bug is tkinter only.


fvogel added on 2017-11-14 22:17:39:

Not sure I understand the issue.

In front of me I have a french physical keyboard. Upper row of letters for this keyboard reads "azerty..."

My Windows Vista OS has french language, and only french language.

I have added an English keyboard layout in Vista, for the french language (see attachment "1Language_with2keyboardlayouts.png"). Upper row of letters for this keyboard reads "qwerty..."

Now:

package require Tk
pack [text .t]

Starting typing in that text widget using the upper row of the physical keyboard, I get "azerty...". Correct.

Now I change the keyboard layout, using the so-called language bar, by clicking on the keyboard icon and selecting the other option. Typing again in the widget using the upper row of letters of the pysical keyboard, I now get "qwerty..." (whereas these keys are physically labelled "azerty...". Again correct!

So changing the keyboard layout works for me?

Note that I did not set two different languages, but really two different keyboards used inside a single language. Or did I screw it up?


dkf added on 2017-11-13 08:36:14:

Note that the fix for this issue should be applied to the 8.5 branch, as well as newer branches, as the workaround is quite user-visible for the affected people.


Attachments: