简体   繁体   English

keyboard.is_pressed() 无法正常工作

[英]keyboard.is_pressed() isn`t working properly

The program works except the keyboard reading, I want when you press escape to end the loop but it ignores it.该程序除了键盘阅读外都可以工作,我想要当您按 Escape 结束循环但它忽略它时。

I tried keyboard.read_key() too but it has the same issue.我也试过keyboard.read_key() 但它有同样的问题。

code:代码:

 for i in range(0, int(n)):
     pyautogui.typewrite(msg + '\n') if n == 0:
     while True:
         for i in range(0, int(n)):
             pyautogui.typewrite(msg + '\n')
         if keyboard.is_pressed("esc"):
             print("You pressed esc")
             break

You're not using is_pressed() correctly.您没有正确使用is_pressed()

It does not return the most recent keypress;它不返回最近的按键; it tells you whether a specific key is pressed right now .它会告诉您当前是否按下特定键。

You have to tell it what key you're looking for:您必须告诉它您要查找的密钥:

if keyboard.is_pressed('esc'):

That function requires an argument -- I don't know how the code worked for you with no arguments. function 需要一个参数——我不知道没有 arguments 的代码如何为您工作。

Heres what you probably meant to do:以下是您可能打算做的事情:

for i in range(0, int(n)):
    if n == 0:
        pyautogui.typewrite(msg + '\n') 

    while not keyboard.is_pressed("esc"): # replace True with this, exits the loop when 'esc' is pressed
        for i in range(0, int(n)):
            pyautogui.typewrite(msg + '\n')
    else:
        print("You pressed esc")

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

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