Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to detect if a key is pressed

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
 
PREVIOUS NEXT
Tagged: #detect #key #pressed
ADD COMMENT
Topic
Name
9+1 =