簡體   English   中英

Python:如果光標處於非活動狀態五分鍾,如何控制光標並在用戶觸摸鼠標時暫停程序(我的 Python 程序)?

[英]Python: How to take control of the cursor if cursor is inactive for five minutes and pause the program (my python program) if user touches the mouse?

我想編寫一個機器人,當用戶離開超過 5 分鍾時將模擬鼠標移動,並在用戶控制即移動鼠標時保持不動。 我編寫了以下代碼作為程序的一部分。 是我的舊程序的鏈接,該程序會定期在給定點單擊。 這個程序的問題是當我想去某個地方時我必須啟動程序,返回后關閉程序才能繼續工作。

這是我為新程序編寫的模塊之一,用於檢測鼠標是否在移動。

import win32api
from time import sleep
print("starting engine.")
count = 0
while(True):
    savedpos = win32api.GetCursorPos()
    if count>20*5:
        break
    sleep(1)
    curpos = win32api.GetCursorPos()
    if savedpos == curpos:
        savedpos = curpos
        print("Mouse is steady.")
    else: 
        print("Mouse is moving.")
    count += 1

我不會編寫代碼,但我有解決問題的想法,您使用 pyautogui.position() 繼續檢查位置,如果它在 300 秒內沒有改變位置,則移動它

我寫了以下代碼,引用其他 Stackoverflow 帖子。 此代碼等待鼠標穩定 4 分鍾。 每當鼠標移動時,計時器都會重置為零。

import win32api
from time import sleep
import pyautogui as gui
print("starting engine.")
count = 0
savedpos = win32api.GetCursorPos()


def actions():
    print(gui.size())
    while True:
        #toast.show_toast("ROBOT V2 !", "Robot is active.", threaded=False, icon_path=None, duration=2)
        gui.click()        
        gui.press('home')
        gui.moveTo(541, 142, 0.25)  # Money Transfer dropdown
        gui.click()
        sleep(0.5)
        gui.moveTo(541, 172, 0.25)  # Money Transfer link
        gui.click()
        sleep(5)
        cond()


def cond():
    count = 0
    while True:
        savedpos = win32api.GetCursorPos()
        sleep(0.5)
        curpos = win32api.GetCursorPos()
        if savedpos == curpos:
            savedpos = curpos
            print("Mouse is steady. Elapsed time: ", (count+1)/2, " seconds.")
            count += 1
            if count >= 480:  # if count is greater than 60 it means timeout will occur after mouse is steady for more than
                # 240 seconds i.e. 4 minutes. (approx.)
                print("User away for more than 4 minutes, taking control of the system.")
                actions()
                break
            else:
                pass
        else:
            print("Mouse is moving.")
            count = 0

cond()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM