按下某个键后,我无法让 keyboard.wait 继续执行代码

I cant get keyboard.wait to continue with the code after pressing a key

我正在 python 中制作一个脚本,在我按下某个键(在本例中该键是 Y)之前不应继续执行该脚本,但每次我到达该键时脚本的一部分并按下它只是过早结束程序的键。我不太清楚我是否遗漏了什么或只是轻度智障。

import random
import time
import keyboard

loop = True
while loop == True:

north = random.randint(0,90)
south = random.randint(0,90)
west = random.randint(0,180)
east = random.randint(0,180)
nors = random.randint(0, 1)
eorw = random.randint(0, 1)

Snorth = str(north)
Ssouth = str(south)
Swest = str(west)
Seast = str(east)

if nors == 1:
    print("north: " , Snorth)
    time.sleep(.5)

else:
    print("south: " , Ssouth)
    time.sleep(.5)

if eorw == 1:
    print("west: " , Swest)
    print("Again?")
    if keyboard.wait('"y"'):
        print("test")
        loop = True
        time.wait(5)

    else:
        loop = False

else:
    print("east: " , Seast)
    print("Again?")
    if keyboard.wait("y"):
        print("test")
        loop = True
        time.wait(5)

    else:
        loop = False

我认为您可能正在寻找 keyboard.read_key() 函数,因为 keyboard.wait() 只会在按下键之前阻止执行,而 returns a NoneType 这在你的 if-else 语句中失败了。

while loop:
    north = random.randint(0,90)
    south = random.randint(0,90)
    west = random.randint(0,180)
    east = random.randint(0,180)
    nors = random.randint(0, 1)
    eorw = random.randint(0, 1)

    if nors == 1:
        print("north: " , str(north))
    else:
        print("south: " , str(south))

    if eorw == 1:
        print("west: " , str(west))
    else:
        print("east: " , str(east))

    print("Again?")
    if keyboard.read_key() == "y":
        loop = True
    else:
        loop = False

来自文档 (https://github.com/boppreh/keyboard#keyboardread_keysuppressfalse):
keyboard.read_key() 块,直到键盘事件发生,然后 returns 该事件的名称,或者,如果丢失, 其扫描码。