6

我知道并使用了很多绑定语法,但是我怎样才能直接检查事件对象并提取按下的字母,例如 'c' 和修饰符,例如,'Control' 和 'Alt' ?

我试过这个

def reportEvent(event):
        eventDict = {
                '2': 'KeyPress', '3': 'KeyRelease', '4': 'ButtonPress', '5': 'ButtonRelease', '6': 'Motion', '7': 'Enter',
                '8': 'Leave', '9': 'FocusIn', '10': 'FocusOut', '12': 'Expose', '15': 'Visibility', '17': 'Destroy',
                '18': 'Unmap', '19': 'Map', '21': 'Reparent', '22': 'Configure', '24': 'Gravity', '26': 'Circulate',
                '28': 'Property', '32': 'Colormap','36': 'Activate', '37': 'Deactivate'}

        rpt = '\n\n%s' % (80*'=')
        rpt = '%s\nEvent: type=%s (%s)' % (rpt, event.type,eventDict.get(event.type, 'Unknown'))
        rpt = '%s\ntime=%s' % (rpt, event.time)
        rpt = '%s widget=%s' % (rpt, event.widget)
        rpt = '%s x=%d, y=%d'% (rpt, event.x, event.y)
        rpt = '%s x_root=%d, y_root=%d' % (rpt, event.x_root, event.y_root)
        rpt = '%s y_root=%d' % (rpt, event.y_root)
        rpt = '%s\nserial=%s' % (rpt, event.serial)
        rpt = '%s num=%s' % (rpt, event.num)
        rpt = '%s height=%s' % (rpt, event.height)
        rpt = '%s width=%s' % (rpt, event.width)
        rpt = '%s keysym=%s' % (rpt, event.keysym)
        rpt = '%s ksNum=%s' % (rpt, event.keysym_num)
        #### Some event types don't have these attributes
        try:
                rpt = '%s focus=%s' % (rpt, event.focus)
        except:
                try:
                        rpt = '%s send=%s' % (rpt, event.send_event)
                except:
                        pass
        print rpt

被盗到Python 和 Tkinter 编程,但它没有显示我正在按下的最终修饰符

4

3 回答 3

8

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

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 键布局的匈牙利苹果键盘上。

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

于 2013-11-08T16:22:02.237 回答
6

在Peter Varo 的想法的帮助下,我有点让它为我工作(运行 windows 10 和 python 3.4)。

from tkinter import *

def onKeyDown(e):
    # The obvious information
    c = e.keysym
    s = e.state

    # Manual way to get the modifiers
    ctrl  = (s & 0x4) != 0
    alt   = (s & 0x8) != 0 or (s & 0x80) != 0
    shift = (s & 0x1) != 0

    # Merge it into an output
    # if alt:
    #     c = 'alt+' + c
    if shift:
        c = 'shift+' + c
    if ctrl:
        c = 'ctrl+' + c
    print(c)

# Run the tk window
root = Tk()
root.bind('<Key>', onKeyDown)
root.mainloop()

在我的情况下,alt键是错误的(它总是被按下),但是ctrlandshift工作正常。

请注意,第一次按ctrlorshift键时,它会将其注册为主键,而不是修饰符。如果您稍后持有它,它只会是一个修饰符。

因此,您确实需要将其作为组合键(如Ctrl+a)来按下。因此,按a然后ctrl会给出一个稍微奇怪的结果(它会忽略 a 并将 ctrl 标记为主键,但不是修饰符)。

于 2015-12-27T15:49:43.293 回答
2

绑定修饰符

以下脚本更正了先前答案中使用的不正确位标志,并考虑到 ex: state=Shift|Alt|Control 不会匹配 Modifier dict 中的任何内容。它需要作为位标志处理并逐位匹配。

任何使用此功能的用户都需要考虑的是,无论您是否要求,都将在 event.state 中报告所有 Lock 键(如果打开)。例如:您打开 CapsLock 和 NumLock 并尝试捕获 Alt + Control 〜您实际上会得到“CapsLock + NumLock + Control + Alt”。解决这个问题的最简单方法是简单地从 Modifier 字典中删除所有 ModN 键。

据我所知,只有通过 keysym 属性才能捕获特定的 _L 和 _R 键。state 属性将始终包含该键的通用修饰符位标志。

#python 3.8
from tkinter import *

root = Tk()

Modifier = {
    0x1    : 'Shift'        ,
    0x2    : 'CapsLock'     ,
    0x4    : 'Control'      ,
    0x8    : 'NumLock'      ,   #Mod1
    0x10   : 'Mod2'         ,   #?
    0x20   : 'ScrollLock'   ,   #Mod3
    0x40   : 'Mod4'         ,   #?
    0x80   : 'Mod5'         ,   #?
    0x100  : 'Button1'      ,
    0x200  : 'Button2'      ,
    0x400  : 'Button3'      ,
    0x800  : 'Button4'      ,
    0x1000 : 'Button5'      ,
    0x20000: 'Alt'          ,   #not a typo
}

root.bind( '<Key>', lambda e: print(' + '.join([v for k, v in Modifier.items() if k & e.state])))
root.mainloop()

绑定事件类型

EventType 是一个枚举。创建一个重复此枚举的整个字典是不必要的。可以通过 event.type.name 获取 EventType 的名称,通过 event.type.value 访问值。如果您绝对必须将 Enum 克隆为 dict,则可以通过以下方式更彻底(更轻松)地完成它:

#python 3.8
# EventType Lookup
BindMap = {i.name:i.value for i in EventType}

# A few aliases that wont be returned from the generator
BindMap['Key']      = '2'
BindMap['Button']   = '4'
BindMap['Double']   = '4'
BindMap['Triple']   = '4'
于 2020-05-25T08:52:36.370 回答