使用python检测鼠标是否正在等待或忙碌

问题描述 投票:0回答:1

我正在使用 Python 2.7 创建一个脚本。该脚本在应用程序内自动执行鼠标单击。

有些情况下,单击鼠标后,鼠标光标会“等待”,我想等到鼠标光标恢复正常后再进入代码中的下一步。

Python中可以检测鼠标是否在等待吗?

python mouse-cursor
1个回答
0
投票

您可以使用 win32gui 库中的 GetCursorInfo 函数来获取光标的状态。

该函数返回的变量之一是游标的句柄。 每当光标改变外观(旋转轮、带光标的旋转轮、光标、十字准线、手形、文本光标等)时,这都会改变。

下面的示例代码获取处于正常状态的光标的句柄,然后等待用户运行一些将光标置于等待状态的代码,然后代码等待直到光标返回到之前的原始(正常)状态继续进行。

from win32gui import GetCursorInfo
import time
cursor_info = GetCursorInfo() #Get the current status of the cursor
normal_handle = cursor_info[1] #Get the normal cursor handle
current_handle = None

#perform task that will make cursor busy
input("Press Enter to continue...")

while current_handle != normal_handle:
    cursor_info = GetCursorInfo() #Get the current status of the cursor
    current_handle = cursor_info[1] #Get the normal cursor handle
    if current_handle != normal_handle:
        print("Cursor is busy. Normal cursor handle: " + str(normal_handle) + ", Current cursor handle: " + str(current_handle) + ".")
    time.sleep(0.5)
print("Cursor is not busy.")

您可以通过硬编码您想要等待的光标句柄类型来使其更加具体。

© www.soinside.com 2019 - 2024. All rights reserved.