您不能直接获得它,但是可以使用一个类,该类将跟随与您要检查的键有关的事件:
import turtle
class WatchedKey:
def __init__(self, key):
self.key = key
self.down = False
turtle.onkeypress(self.press, key)
turtle.onkeyrelease(self.release, key)
def press(self):
self.down = True
def release(self):
self.down = False
# You can now create the watched keys you want to be able to check:
a_key = WatchedKey('a')
b_key = WatchedKey('b')
# and you can check their state by looking at their 'down' attribute
a_currently_pressed = a_key.down
每次单击窗口时,都会显示一个小样,状态为“ a”和“ b”:
def print_state(x, y):
print(a_key.down, b_key.down)
screen = turtle.Screen()
screen.onclick(print_state)
turtle.listen()
turtle.mainloop()
turtle.done()
如果要遵循一堆键的状态,则可以这样创建其监视程序:
keys_to_watch = {'a', 'b', 'c', 'd', 'space']
watched_keys = {key: WatchedKey(key) for key in keys_to_watch}
并使用
b_pressed = watched_keys['b'].down