【发布时间】:2021-08-12 17:38:30
【问题描述】:
我当前的代码:
from pynput import keyboard
from pynput.keyboard import Key, Controller
import threading
import datetime, time
import os
import subprocess
keyboard = Controller()
def loading():
os.system("open /System/Applications/TextEdit.app")
time.sleep(1)
while running:
keyboard.press("a")
def on_press(key):
global running # inform function to assign (`=`) to external/global `running` instead of creating local `running`
if key == keyboard.Key.left:
running = True
# create thread with function `loading`
t = threading.Thread(target=loading)
# start thread
t.start()
if key == keyboard.Key.down:
# to stop loop in thread
running = False
if key == keyboard.Key.right:
# stop listener
return False
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
我试图循环键盘按下,但在按下右键或向下键时停止,但我收到“'Controller' object has no attribute 'Listener'”的错误。当我不包含侦听器部分时,我的代码可以工作,所以我不确定如何将它们组合在一起以制作一个有凝聚力的程序。
【问题讨论】:
标签: python-3.x pynput