2

我正在使用 Python 2.5 编写一个 TkInter 应用程序,我需要找出大写锁定和 shift 键的状态(真或假)。我已经搜索了整个网络,但找不到解决方案。

4

4 回答 4

3

Tkinter 中的键盘事件可能很棘手。

我建议您按顺序查看以下内容:

这是一个显示 keycode 和 state 事件参数值的程序。你可以用它来做实验。单击窗口,然后敲击键盘。

from Tkinter import *
root = Tk()

def key(event):
    print "Keycode:", event.keycode, "State:", event.state

def callback(event):
    frame.focus_set()
    print "clicked at", event.x, event.y

frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.bind("<Button-1>", callback)
frame.pack()

root.mainloop()
于 2009-03-20T10:16:34.457 回答
1

LockShift事件修饰符:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html

于 2009-03-20T10:08:20.843 回答
0

我用谷歌搜索了一个..我不确定它是否适用于所有键...

http://www.java2s.com/Code/Python/Event/KeyactionFunctionKeyALtControlShift.htm

于 2009-03-20T10:11:50.380 回答
0

利用:

from Tkinter import *
root = Tk()
caps_lock_on = False
def CAPSLOCK_STATE():
    import ctypes
    hllDll = ctypes.WinDLL ("User32.dll")
    VK_CAPITAL = 0x14
    return hllDll.GetKeyState(VK_CAPITAL)

CAPSLOCK = CAPSLOCK_STATE()
if ((CAPSLOCK) & 0xffff) != 0:
    print "\nCaps lock is on\n"
    caps_key_on = True
else:
    caps_key_on = False
    print 'Caps lock is off'

def caps_lock_pressed(event=''):
    global caps_lock_on
    if caps_lock_on == False:
        caps_lock_on = True
        print 'Caps lock is on'
    else:
        caps_lock_on = False
        print 'Caps lock is off'

#Changes if shift key is on and off 
def shift_key_pressed(event=''):
    global shift_key_on
    shift_key_on = True
    print 'Shift is being holded' 

def shift_key_released(event=''):
    global shift_key_on
    shift_key_on = False
    print 'Shift has been released'
        
root.bind('<Caps_Lock>',caps_lock_pressed)
root.bind('<Shift_L>',shift_key_pressed)
root.bind('<Shift_R>',shift_key_pressed)
root.bind('<KeyRelease-Shift_R>',shift_key_released)
root.bind('<KeyRelease-Shift_L>',shift_key_released)
root.mainloop()

这将检查它的大写锁定,然后它将绑定大写锁定并转换以更改状态。大写锁定检测系统是借用的。

于 2020-11-02T17:13:19.907 回答