1 回答
TA贡献1942条经验 获得超3个赞
当执行某些会阻止代码的任务时,您可以使用线程来运行代码。(在您的代码中,sleep(1)将阻止代码),无论如何,这在我的电脑上运行良好:
from pynput.mouse import Listener
import time
import threading
def task(): # this is what you want to do.
time.sleep(1) # <<< Tried to add it over here cuz it takes most of the process.
print("After sleep 1 second")
def on_move(x, y):
print('Pointer moved to {0}'.format((x, y)))
threading.Thread(target=task).start() # run some tasks here.
def on_click(x, y, button, pressed):
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
if not pressed:
return False
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format((x, y)))
with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()
添加回答
举报