0

When I switch the keyboard to Russian layout, my widget gets key events with some diacritics in keysym instead of Russian letters.

Here's a demonstration (using the dump module):

import Tkinter as tkinter
from dump import dump

def show_event(e):
    dump(e)

root=tkinter.Tk()
lb = tkinter.Listbox(root)
lb.grid()
root.bind_class('Listbox','<Control-Key>', show_event)
root.mainloop()

Now, when I focus the listbox and press Ctrl with letters фыва (that correspond to asdf), the keysyms in the printed events are: ocircumflex,ucircumflex,acircumflex,agrave.

While I, naturally, expected to get Cyrillic_ef, Cyrillic_yeru etc.

At the same time, if I type text into an Entry, Russian letters are inserted correctly.

Any ideas of what is going on & how to fix? That said, maybe it doesn't even need fixing (it may allow one to use the same binding for any non-English keyboard layout) but I'd still like to know the reason for this highly counterintuitive effect incl. if it's by design.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152

1 Answers1

1

From what I've found, it depends on what system you're working with. It seems like from the answers here, for Windows you have to hack together things like '<Control-ntilde>' in key bindings, so you might be able to do '<Control-ocircumflex>', '<Control-ucircumflex>', ...

In OSX, I'm not even sure how to go about this. I tried the example that works for Windows and wasn't able to get it to register the keypresses. And for Linux, I haven't been able to try.

EDIT: To actually answer your question, essentially when Tk translates virtual key codes delivered by the system to the Tk window, it uses the function TkpGetKeySym(), which in turn calls Windows ToAscii() function. This function will take into account the current active keymap when processing even though it's only designed to return codes of Latin-1 characters. The specific keymap for Russian is Windows-1251, which is used when TkpGetKeySym() is called but the caller interprets the outputs as if they're Latin-1. Is there a fix? It seems not. I've basically just (attempted to) summarize what's from this page, so I'd suggest reading it for a more in-depth explanation and to check if I misread anything.

  • _"so you might be able to do '', ''"_ -- I already figured out that much. I'd like to know where these strange symbols are coming from (perhaps some "default" layout?) and if something can be done about them. Also why just inputting national characters works correctly at the same time. – ivan_pozdeev Jul 25 '18 at 03:06
  • I've edited my answer to answer your questions. Hopefully this helps! –  Jul 25 '18 at 23:32
  • Now you're talking! Although the info is a bit obsolete: since https://github.com/tcltk/tk/commit/b95cd239d1b396c8d302561387003af38bdd0a5c (8.6.7+), `ToAscii` is replaced by `ToUnicode`. – ivan_pozdeev Jul 26 '18 at 03:49
  • I've been searching for the past few days within the source and haven't been able to find why though I'd guess the caller is interpreting the unicode wrong but my guess is as good as any? I'll be gone for the next few weeks, so there won't be an answer soon. –  Jul 28 '18 at 01:58