且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查是否在 tkinter 中按下了修饰键

更新时间:2023-12-03 09:15:40

理论上这将是您问题的答案:

Theoretically this would be the answer for your problem:

from tkinter import *

root = Tk()

mods = {
    0x0001: 'Shift',
    0x0002: 'Caps Lock',
    0x0004: 'Control',
    0x0008: 'Left-hand Alt',
    0x0010: 'Num Lock',
    0x0080: 'Right-hand Alt',
    0x0100: 'Mouse button 1',
    0x0200: 'Mouse button 2',
    0x0400: 'Mouse button 3'
}

root.bind( '<Key>', lambda e: print( 'Key:', e.char,
                                     'Mods:', mods.get( e.state, None )))

root.mainloop()

但是,它无法正常工作——或者至少在我的 110 键布局的匈牙利苹果键盘上没有.

However, it is not working as it should be -- or at least it's not on my hungarian apple keyboard which is a 110 keys layout..

无论如何,这里是事件对象的所有属性:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-handlers.html

Anyway, here are all the properties of the event object: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-handlers.html