1

Is there a way to detect current keyboard layout in Tkinter?

It is necessary to process keyboard accelerators correctly for such languages as Russian, French, Greek (see my other question for details).

Community
  • 1
  • 1
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • The report linked in your question does suggest an approach to it, did you check it at http://bugs.python.org/issue1794#msg86386 ? If that doesn't help you, explain why. Also, do you know how to correctly detect the current keyboard layout using anything else ? I remember Ubuntu having a hard time to detect even my mostly typical keyboards sometimes. – mmgp Jan 23 '13 at 05:11
  • I quoted my workaround which is similar to the recipe given in your link as an [answer](http://stackoverflow.com/a/14474957/237105) to my original question along with my ideas over why it could be bad. – Antony Hatchkins Jan 23 '13 at 08:11
  • As for determining keyboard layout in general - at least in Windows it is pretty feasible: http://stackoverflow.com/questions/1291509/how-to-get-the-code-page-of-the-current-keyboard-layout?rq=1 – Antony Hatchkins Jan 23 '13 at 08:13
  • That is very different from determining the keyboard layout, that is simply returning the active layout. What if the active layout is wrong ? Your workaround is not similar, it is completely different. – mmgp Jan 23 '13 at 12:32
  • In the mentioned workaround (msg86386) gpolo sets up a function and catches a keycode that corresponds to the necessary physical button. I do the same thing, although I translated the keycode back to verbose 'keysym' (for example see a translation table [here](http://www.tcl.tk/man/tcl8.5/TkCmd/keysyms.htm) and catch it in the ordinary way. Maybe I misunderstood you? – Antony Hatchkins Jan 23 '13 at 12:54
  • There is in fact no general solution built in Tk currently. First I though the workaround could simply be extended to consider `event.char` too, but I don't think that will work either. – mmgp Jan 23 '13 at 13:33
  • I looked through all the fields of the `event` and none corresponded to the scancode (position of the key on the keyboard) – Antony Hatchkins Jan 23 '13 at 13:39

2 Answers2

1

Seems that keysym is "??" if layout is not english.

def key_callback(e):
    # Generate Ctrl-V if current layout is not english
    if e.state & 4 > 0 and chr(e.keycode) == 'V' and e.keysym == "??":
        root.focus_get().event_generate("<<Paste>>")
root.bind("<Key>", key_callback)
Winand
  • 2,093
  • 3
  • 28
  • 48
0

No, there is no such feature built into Tkinter.

Why not simply have your application ask the user where each special key is, the first time the app starts up? Apple does this with OSX -- it asked me to press a couple of keys the very first time I booted up a fresh install.

You can put the focus into a widget and bind to <Any-KeyPress>, and from that grab enough information to bind to whatever key they pressed.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • An example application for wxPython performs "translation" of keyboard accelerators flawlessly without any intervention on my side (under Windows). And yet you say that it is impossible to accomplish in Tkinter. – Antony Hatchkins Jan 23 '13 at 12:57
  • To ask user every single keyboard accelerator is unnecessary burden. I've only seen it in `mc` under linux and that's one of the worst things about midnight commander ) – Antony Hatchkins Jan 23 '13 at 12:59
  • > You can put the focus Yep, that's what I currently do for that, and the result is in my [answer](http://stackoverflow.com/a/14474957/237105) to the original question – Antony Hatchkins Jan 23 '13 at 12:59