对于一个项目,我正在通过数字键盘完全控制程序。很多依赖于数字键盘上的按钮能够被检测为被按下或不被按下;我在按下时将布尔值设置为true,在释放时将其设置为false。有了这些限制,我遇到了一个问题:每次按下时, Num-Lock 键在向上和向下之间切换,在释放时没有任何内容。使用我用于其他键的相同系统,按一次键将其变为true并再次按下它将使其再次变为假,而不是按下它使其成为真,并释放它再次使其变为假。
基本上,我怎样才能获得numpad Num-Lock 键,在按下和释放时触发某些内容而不是每次按下时切换?
预期操作系统:Windows和Linux(Raspberry Pi)
Python 3.x
我得到的输入与覆盆子pi上的Python游戏相同,使用event.type
和event.key
等。
答案 0 :(得分:1)
Tkinter
内的事件处理程序可能会对您有所帮助:事件具有以下属性:
# <event>.char on-{ <KeyPress> | <KeyRelease> }
# .keysym on-{ <KeyPress> | <KeyRelease> }
# .keysym_num on-{ <KeyPress> | <KeyRelease> }
# .height on-{ <Configure> }
# .width on-{ <Configure> }
# .num on-{ <Mouse-1> | <Mouse-2> | ... } ? 4,5 == <MouseWheel>
# .serial <-- system-assigned Integer
# .time <-- system-assigned Integer ( .inc each msec )
# .widget <-- system-assigned <widget>-instance
# .x <-- system-assigned <Event>-in-<widget>-mouse-location.x
# .y <-- system-assigned <Event>-in-<widget>-mouse-location.y
# .x_root <-- system-assigned <Event>-on-<Screen>-mouse-location.x
# .y_root <-- system-assigned <Event>-on-<Screen>-mouse-location.y
这个模拟演示可以向您展示机制:
#---------------------------------------------------------------# EVENT HANDLER DEMO:
# from Tkinter import * # import Tkinter as tk
root = Tk() # root = tk.Tk()
root.lift()
#-------------------------------------------------------
>>> def aMouseEnterHANDLER( anEvent ):
... print "<Enter>-event: ", anEvent.serial, anEvent.time, anEvent.x, anEvent.y, str( anEvent.widget )
... print
...
>>> def KeyHANDLER( anEvent ):
... print "<Key>-event: ", anEvent.serial, anEvent.time, anEvent.char, anEvent.keysym, anEvent.keysym_num, str( anEvent.widget )
...
>>> root.bind( "<Enter>", aMouseEnterHANDLER )
'23852056EnterHandler'
>>> root.bind( "<KeyPress>", KeyHANDLER ) # root-widget level .bind()
'23852576KeyHandler'
>>> root.mainloop() # <-- after entering this, <KeyPress> started to work ----------------------
<Key>-event: 904 590059546 s s 115 .
<Enter>-event: 911 590065062 1 5 .
<Enter>-event: 911 590065062 1 5 .12965248
<Enter>-event: 915 590067703 4 1 .
<Enter>-event: 915 590067703 4 1 .12965248
<Key>-event: 918 590069062 s s 115 .
<Key>-event: 920 590069906 s s 115 .
<Key>-event: 922 590070390 Caps_Lock 65509 .
<Key>-event: 924 590071687 Shift_L 65505 .
<Key>-event: 925 590072187 Shift_L 65505 .
<Key>-event: 926 590072218 Shift_L 65505 .
<Key>-event: 927 590072250 Shift_L 65505 .
<Key>-event: 928 590072281 Shift_L 65505 .
<Key>-event: 929 590072328 Shift_L 65505 .
<Key>-event: 930 590072359 Shift_L 65505 .
<Key>-event: 931 590072390 Shift_L 65505 .
<Key>-event: 932 590072421 Shift_L 65505 .
<Key>-event: 934 590072890 A A 65 .
<Key>-event: 936 590073390 A A 65 .
<Key>-event: 938 590073531 A A 65 .
<Key>-event: 940 590073718 A A 65 .
<Key>-event: 942 590074093 Shift_L 65505 .
<Key>-event: 944 590074625 Shift_L 65505 .
<Key>-event: 946 590074843 Shift_L 65505 .
<Key>-event: 948 590075031 Shift_L 65505 .
<Key>-event: 950 590075656 Shift_L 65505 .
<Key>-event: 952 590076031 Caps_Lock 65509 .
<Enter>-event: 956 590109296 0 1 .
<Enter>-event: 956 590109296 0 1 .12965248
<Enter>-event: 959 590109734 6 5 .12965248
<Enter>-event: 962 590110078 9 9 .
<Enter>-event: 962 590110078 9 9 .12965248
<Enter>-event: 965 590110328 9 10 .12965248
<Enter>-event: 968 590111781 40 19 .
<Key>-event: 968 590113875 Up 65362 .
<Key>-event: 970 590114781 Left 65361 .
<Key>-event: 972 590116718 + plus 43 .
<Key>-event: 974 590117656 - minus 45 .
Return 65293 .976 590118343
<Key>-event: 978 590120359 d d 100 .
<Key>-event: 980 590123312 Tab 65289 .
<Key>-event: 988 590124140 Tab 65289 .12965248
<Key>-event: 988 590124140 Tab 65289 .12965248
<Key>-event: 990 590125578 Tab 65289 .12965248
<Key>-event: 990 590125578 Tab 65289 .12965248