简体   繁体   English

Python - Turtle.onkey() - 一种接受任何/未知密钥的方法?

[英]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.我还想避免对键盘上的每个键都有单独的 onkey 调用和函数。

Is there a way to Get the key pressed from the onkey event without separate events for each key?有没有办法从 onkey 事件中获取按下的键,而无需为每个键单独设置事件?

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.据我所知,单独使用 Turtles 是不可能的。 You can use the same handler for all keypresses by passing '' to onKey您可以通过将 '' 传递给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.您可以在getKey()函数中使用不同的库(例如Getch getKey()来查看当时按下了哪个键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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