Python - win32api 光标脚本,自动移动光标,点击并返回
Python - win32api cursor script that automatically moves a cursor, clicks and brings it back
我有一个检测鼠标点击的脚本。我需要它:[1。将我的光标移动到一个位置 2. 单击该位置 3. 将其移回原始位置] 每当我单击屏幕上的任何位置时,都会 运行。问题是,脚本创建的点击仍然被注册并创建一个来回循环。我怎样才能改变这种行为,使它不会 运行 当点击发生在脚本而不是我身上时,它不会发挥作用?
import win32api
import time
state_left = win32api.GetAsyncKeyState(0x01) # LMB down is 0 or 1, LMB up is -127 or -128
time.sleep(2)
while True:
a = win32api.GetAsyncKeyState(0x01)
if a != state_left:
state_left = a
print(a)
if a < 0:
print('Left Button Pressed')
else:
print('Left Button Released')```
根据document,您可以使用GetAsyncKeyState
:
The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON).
因此只有在鼠标物理状态改变时才会触发,可以使程序正常运行。
两次触发的原因是a
的值设置错误。只需要通过GetAsyncKeyState
函数获取鼠标状态的最高位,如下代码所示:
c = 1
state_left = win32api.GetAsyncKeyState(0x01) # LMB down is 0 or 1, LMB up is -127 or -128
while True:
a = win32api.GetAsyncKeyState(0x01) & 0x8000
if a != state_left:
state_left = a
print(a)
if a != 0:
print('Left Button Pressed')
else:
print('Left Button Released')
before = win32api.GetCursorPos()
win32api.SetCursorPos((1300, 900))
pyautogui.click()
win32api.SetCursorPos(before)
time.sleep(0.001)
更多参考:
我有一个检测鼠标点击的脚本。我需要它:[1。将我的光标移动到一个位置 2. 单击该位置 3. 将其移回原始位置] 每当我单击屏幕上的任何位置时,都会 运行。问题是,脚本创建的点击仍然被注册并创建一个来回循环。我怎样才能改变这种行为,使它不会 运行 当点击发生在脚本而不是我身上时,它不会发挥作用?
import win32api
import time
state_left = win32api.GetAsyncKeyState(0x01) # LMB down is 0 or 1, LMB up is -127 or -128
time.sleep(2)
while True:
a = win32api.GetAsyncKeyState(0x01)
if a != state_left:
state_left = a
print(a)
if a < 0:
print('Left Button Pressed')
else:
print('Left Button Released')```
根据document,您可以使用GetAsyncKeyState
:
The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON).
因此只有在鼠标物理状态改变时才会触发,可以使程序正常运行。
两次触发的原因是a
的值设置错误。只需要通过GetAsyncKeyState
函数获取鼠标状态的最高位,如下代码所示:
c = 1
state_left = win32api.GetAsyncKeyState(0x01) # LMB down is 0 or 1, LMB up is -127 or -128
while True:
a = win32api.GetAsyncKeyState(0x01) & 0x8000
if a != state_left:
state_left = a
print(a)
if a != 0:
print('Left Button Pressed')
else:
print('Left Button Released')
before = win32api.GetCursorPos()
win32api.SetCursorPos((1300, 900))
pyautogui.click()
win32api.SetCursorPos(before)
time.sleep(0.001)
更多参考: