screen.onkey 使用 Shift 键

问题描述

我正在使用来自乌龟的 screen.onkey 在 Python 中编写一些代码。有什么方法可以使用键盘上的 Shift 键触发事件。

   import turtle
walk_speed=10
def sprint():
    global walk_speed
    walk_speed+=8
def slow():
    global walk_speed
    walk_speed-=8
screen.onkeypress(sprint,"Shift")
screen.onkeyrelease(slow,"Shift")

该程序在空格键或指定的字母键下运行良好。这里有什么办法可以使用shift键吗?

解决方法

不,不能使用 screen.onkeypress(sprint,"Shift") 您当然可以导入键盘模块。最终代码:

import turtle
import keyboard
walk_speed=10
def sprint():
    global walk_speed
    walk_speed+=8
def slow():
    global walk_speed
    walk_speed-=8
while True:
    if keyboard.is_pressed('shift'):
        sprint()
    if keyboard.is_pressed('shift'):
        slow()