codingCat
codingCat

Reputation: 2406

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?

Upvotes: 0

Views: 608

Answers (1)

Sam MacGinty
Sam MacGinty

Reputation: 36

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.

Upvotes: 1

Related Questions