简体   繁体   中英

Python - Turtle.onkey() - A way to accept Any/Unknown Key?

I would like to build a little typing/keyboard demo of the turtles key event. I would also like to avoid having a separate onkey call and function for every single key on the keyboard.

Is there a way to Get the key pressed from the onkey event without separate events for each key?

Something like:

def getKey(key):
  turtle.write(key)

turtle.onkey(getKey,None)
turtle.listen()

Possible?

From what I can see it is not possible using Turtles alone. You can use the same handler for all keypresses by passing '' to onKey

def getKey():
  turtle.write('Key pressed')

turtle.onkey(getKey,'')
turtle.listen()

You may be able to use a different library such as Getch within the getKey() function to see which key if being pressed at the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM