Turtle-graphics Keypress Event Not Repeating
Solution 1:
Ok, I've found the solution.
The Trinket (JS) implementation is inaccurate.
Using this code: wn.onkey(go_up, "Up")
binds the function to the key-press event in "standard" Python, which means the function is only called once.
To have it repeat while the key is held down, the following is needed:
def move(event):
my_turtle.forward(5)
turtle.getcanvas().bind('<Up>', move)
Solution 2:
is there a way to make it work on Windows so that holding a key down creates repeat events?
Key repeat is an operating system function and different systems handle it differently. For example, the keys on my Apple OS X system repeat by default but I can go into System Preferences / Keyboard and change the rate and even turn it off (which I did to test the code below.)
If you don't have key repeat and want to simulate it, here's a simple example of using a turtle timer event to start a key repeating on key down and turn it off on key up:
# import the turtle module so we can use all the neat code it containsfrom turtle import Turtle, Screen
KEY_REPEAT_RATE = 20# in milliseconds# Create a variable `tina` that is a Turtle() object. Set shape to 'turtle'
tina = Turtle('turtle')
# Create a variable `screen`, a Screen() object, that will handle keys
screen = Screen()
# Define functions for each arrow keydefgo_left():
tina.left(7)
if repeating:
screen.ontimer(go_left, KEY_REPEAT_RATE)
defgo_right():
tina.right(7)
if repeating:
screen.ontimer(go_right, KEY_REPEAT_RATE)
defgo_forward():
tina.forward(10)
if repeating:
screen.ontimer(go_forward, KEY_REPEAT_RATE)
defgo_backward():
tina.backward(10)
if repeating:
screen.ontimer(go_backward, KEY_REPEAT_RATE)
defstart_repeat(func):
global repeating
repeating = True
func()
defstop_repeat():
global repeating
repeating = False
repeating = False# Tell the program which functions go with which keys
screen.onkeypress(lambda: start_repeat(go_left), 'Left')
screen.onkeyrelease(stop_repeat, 'Left')
screen.onkeypress(lambda: start_repeat(go_right), 'Right')
screen.onkeyrelease(stop_repeat, 'Right')
screen.onkeypress(lambda: start_repeat(go_forward), 'Up')
screen.onkeyrelease(stop_repeat, 'Up')
screen.onkeypress(lambda: start_repeat(go_backward), 'Down')
screen.onkeyrelease(stop_repeat, 'Down')
# Tell the screen to listen for key presses
screen.listen()
screen.mainloop()
However, your challenge is to figure out when you need this simulated repeat and when the system already has repeat capability -- having both active will be messy.
Post a Comment for "Turtle-graphics Keypress Event Not Repeating"