简体   繁体   English

keyboard.is_pressed() 在 Python 中是如何工作的?

[英]How does keyboard.is_pressed() work in Python?

I'm trying to figure out when i press 'u' only 1 times, why is it pressing 'w' infinite times.我想弄清楚当我只按 'u' 1 次时,为什么要无限次按 'w'。 Print functions don't work neither but if i delet the keyboard.press('w') and keyboard.release('w'), the print functions start to work correctly ( it's printing out 4 until i press a button then it prints out the right number and when i release the button it writes out 4 again)打印功能也不起作用,但如果我删除 keyboard.press('w') 和 keyboard.release('w'),则打印功能开始正常工作(它打印出 4,直到我按下一个按钮,然后它才会打印写出正确的数字,当我松开按钮时,它再次写出 4)

while True:
    if keyboard.is_pressed('u'):
        keyboard.press('w')
        keyboard.release('w')
        print(0)
    elif keyboard.is_pressed('j'):
        #keyboard.press_and_release('s')
        print(1)
    elif keyboard.is_pressed('k'):
         #keyboard.press_and_release('d')
         print(2)
    elif keyboard.is_pressed('h'):
        #keyboard.press_and_release('a')
        print(3)
    else:
        print(4)
        keyboard.release('w')

I know that it was a year ago, but I have found this question today.我知道那是一年前,但我今天发现了这个问题。

Sooo, according to the Keyboard API : Sooo,根据键盘 API
keyboard.press(hotkey) emulates pressing a hotkey on keyboard. keyboard.press(hotkey)模拟按键盘上的热键。
keyboard.release(hotkey) emulates releasing a hotkey on keyboard. keyboard.release(hotkey)模拟在键盘上释放热键。
A hotkey is an key number, key name, or combination of two or more keys.热键是键号、键名或两个或多个键的组合。

keyboard.press_and_release(hotkey) or keyboard.send(hotkey) emulates pressing and releasing a key or hotkey. keyboard.press_and_release(hotkey)keyboard.send(hotkey)模拟按下和释放一个键或热键。
Pressing and releasing behaviour depends on values of do_press and do_release when using keyboard.send() .按下和释放行为取决于使用keyboard.send()do_pressdo_release值。

For example:例如:

keyboard.send('space', do_press=True, do_release=True)

will emulate pressing and releasing space key , but:将模拟按下释放空格键,但是:

keyboard.send('space', do_press=False, do_release=True)

will emulate only releasing space key .模拟释放空格键

keyboard.is_pressed(key) returns True if a specified key has been pressed, and False otherwise如果指定的被按下, keyboard.is_pressed(key)返回 True,否则返回 False


Hope I've helped!希望我有所帮助!

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

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