Tkinter international bind

前端 未结 5 1397
清酒与你
清酒与你 2020-12-11 17:28

Is there a way in Tkinter to bind a combination of keys that will work in all keyboard layouts? (bind by scancode)

For example, I need \'Control-Z\' bin

5条回答
  •  眼角桃花
    2020-12-11 18:04

    Thanks to @acw1668 for help!

    You need to do something like this to use hotkeys with any language layout (the callback from this example is running when Control key is pressed, and prints the key that is pressed in the same time with Control:

    from tkinter import *
    
    
    def callback(event):
        if (event.state & 4 > 0):
            print("Ctrl-%s pressed" % chr(event.keycode))
    
    root = Tk()
    root.bind("", callback)
    root.mainloop()
    

    PS: This example was checked when English, Russian, Ukrainian, Arabic, Amharic, Armenian, Greek, Georgian, French, Chinese, Japanese and other language layouts were used.

提交回复
热议问题