亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使 pynput 鼠標偵聽器減少資源消耗

使 pynput 鼠標偵聽器減少資源消耗

慕村225694 2023-07-18 10:20:32
我正在嘗試使用pynput 中的此腳本來監視我的鼠標,但它太占用資源了。嘗試import time添加time.sleep(1)后on_move(x, y)功能,但當你運行它時,你的鼠標會發瘋。這是整體代碼:import timedef on_move(x, y):? ? print('Pointer moved to {0}'.format((x, y)))? ? time.sleep(1) # <<< Tried to add it over here cuz it takes most of the process.def on_click(x, y, button, pressed):? ? print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))? ? if not pressed:? ? ? ? return Falsedef 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()
查看完整描述

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()


查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號