While playing around with Python's Turtle module, I used some key events as the official documentation states:
turtle.onkey(fun, key)
Parameters:
fun
– a function with no arguments or Nonekey
– a string: key (e.g. “a”) or key-symbol (e.g. “space”)
Now the interesting thing is that when you call 1) the onkeyrelease()
method and pass a not registered string (like an empty one (""
), or "+"
, etc.) as key
parameter:
turtle.onkeyrelease(lambda: print("Got key event while listening to none."), "")
No matter what key is pressed by the user, the program outputs "Got key event ...
", which was by the way the problem in this question.
Unfortunately I can't find more information about this behavior in the documentation ore elsewhere on the internet. So I wonder if there is a complete list of all supported key-name-strings used to program key events?
1) The basic setup used in the question:
import turtle
turtle.setup(700,500)
turtleWindow = turtle.Screen()
turtleWindow.onkey(lambda: print("You pressed 'a'"), "a")
turtleWindow.listen()
I scanned through the turtle.py
source and came to the same conclusion as mgc, that the keys are part of tkinter
, not turtle
. Not wanting to read through the entire tkinter
source, I did some googling and found this full list of keysyms in the Tk docs, as well as this abbreviated list for Latin-1 keyboards (it is missing the individual letters, but they are also valid key identifiers, such as "Q"
). I'm not sure if they are case-sensitive or not, so you'll have to do some experimentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With