如何使用 PyAutoGUI 检测按键事件?

Mi *_*res 7 python-2.7 pyautogui

如何使用 PyAutoGUI 检测按键事件?在我的研究中,我无法在这个模型中举例:

import pyautogui

num = 0
if pyautogui.press('b'): # I know the right thing is not to use the press, but, what do I put in place?
    num = 1
Run Code Online (Sandbox Code Playgroud)

小智 10

python键盘库提供了您需要的确切功能:

import keyboard

if keyboard.is_pressed('b'):
    num = 1
Run Code Online (Sandbox Code Playgroud)

  • 与“pynput”的 GPL 相比,“keyboard”lib 使用 MIT 许可证。但请注意,在 `keyboard` lib (a) 中,“Linux 部分读取原始设备文件 (/dev/input/input*),但这需要 root”,并且 (b) OS X 支持是实验性的。 (2认同)

Al *_*art 6

在 PyAutoGUI 中无法检测击键。您可能想尝试使用 Pynput 模块:https ://pypi.org/project/pynput/