python里怎么判定键盘事件
提问:python里怎么判定键盘事件
比如按下键盘上的'a'键,turtle库接收事件后执行turtle.write('a')命令,画出字母'a'按下'a'和'Shift'键,turtle相应地画出'A'ps:就只有python,没有其他东西例如pygame或者其他啥的
网友回答:
首先,turtle就不是python本身的,它也是第三方的库
其次,这个就可以用turtle实现,如(只是参考,没考虑换行等)
import turtle
def OnKeyA():
turtle.write("A", font=('Arial', 18, 'normal'))
turtle.forward(15)
def OnKeya():
turtle.write("a", font=('Arial', 18, 'normal'))
turtle.forward(15)
turtle.Screen().onkey(OnKeyA, 'A')
turtle.Screen().onkey(OnKeya, 'a')
turtle.Screen().listen()
turtle.mainloop()
运行测试