我正在尝试将自动光标定位与pyWinhook结合起来,在多屏幕桌面设置上实现光标环绕。最终,我希望当我点击屏幕的最右边或最左边边缘时,扩展代码的功能,将其包装到不同的系统,但现在我正在通过尝试在单个系统上使其正常工作来忽略网络位
最初,我使用pyautogui编写了一个简单的脚本来进行包装:
import pyautogui
MAX_X, MAX_Y = pyautogui.size()
MAX_X -= 1
MAX_Y -= 1
def detect_wrap():
x, y = pyautogui.position()
if x >= MAX_X:
pyautogui.moveTo(0,y)
elif x <= 0:
pyautogui.moveTo(MAX_X,y)
elif y >= MAX_Y:
pyautogui.moveTo(x, 0)
elif y <= 0:
pyautogui.moveTo(x, MAX_Y)
return
print(pyautogui.size())
while True:
detect_wrap()
它工作得很好,但在结尾有一个讨厌的无休止的循环,所以我试图寻找一种更干净的方法来处理它,偶然发现了pyWinhook和pythoncom,并编造了一个类似的脚本来实现同样的目标。但是,当我这样做的时候,我没有从最底部跳到顶部,从最右边跳到最左边,等等。脚本粘在了边缘并停留在那里。虽然我不知道为什么。我把一些打印的语句放进去,试图让大家了解一下,我看到鼠标确实移动到了相反的边界,但有什么东西一直让它返回。我潜入pyautogui并了解它是如何移动光标的(使用ctypes.windell.user32.SetCursorPos(xpos,ypos)),因此我将pyautogui从等式中剔除。我剩下的是这样的(请注意,这是一项正在进行的工作):
import win32api
import pyWinhook as pyHook
import pythoncom
import ctypes
import win32api, win32con
import win32.win32gui as win32gui
import os, sys, time
SCREEN_WIDTH = 0 # This will actually be set later, when we know the size of the attached screen(s)
SCREEN_HEIGHT = 0 # This will actually be set later, when we know the size of the attached screen(s)
XMAX = 0 # This will actually be set later, when we know the size of the attached screen(s)
YMAX = 0 # This will actually be set later, when we know the size of the attached screen(s)
XMIN = 1
YMIN = 1
def setWrapPos(xpos, ypos, border):
win32gui.ReleaseCapture()
if border == 'left':
xpos = SCREEN_WIDTH - 1
elif border == 'top':
ypos = SCREEN_HEIGHT - 1
elif border == 'right':
xpos = XMIN
elif border == 'bottom':
ypos = YMIN
else:
print('ERROR: Illegal border passed to setWrapPos()')
ctypes.windll.user32.SetCursorPos(xpos, ypos)
time.sleep(0.01)
return
def onclick(event):
print('Detected mouseclick at (%d,%d)' % event.Position)
return True
def trackMouse(event):
hm.UnhookMouse()
flags, hcursor, coords = win32gui.GetCursorInfo()
xpos, ypos = coords
print("Mouse at (%d, %d)" % (xpos, ypos))
if (xpos <= XMIN):
setWrapPos(xpos, ypos, 'left')
elif (ypos <= YMIN):
setWrapPos(xpos, ypos, 'top')
elif (xpos >= XMAX):
setWrapPos(xpos, ypos, 'right')
elif (ypos >= YMAX):
setWrapPos(xpos, ypos, 'bottom')
flags, hcursor, coords = win32gui.GetCursorInfo()
xpos, ypos = coords
print("Mouse moved to (%d, %d)" % (xpos, ypos))
hm.HookMouse()
return True
def onWinCombo(event):
if event.Key == 'X':
print('Lcontrol-X was detected. Exiting.')
hm.UnhookMouse()
hm.UnhookKeyboard()
os._exit(0)
else:
hm.KeyDown = onKeyboardEvent
return False
def noMoreCombo(event):
hm.KeyDown = onKeyboardEvent
hm.KeyUp = None
return False
def onKeyboardEvent(event):
print('Keyboard action detected, ' + str(event.Key) + ' was pressed.')
if str(event.Key) == 'Lcontrol':
print('Lcontrol detected.')
hm.KeyDown = onWinCombo
hm.KeyUp = noMoreCombo
return True
curr_right = 0
mons = win32api.EnumDisplayMonitors()
i = 0
while i < win32api.GetSystemMetrics(win32con.SM_CMONITORS):
minfo = win32api.GetMonitorInfo(mons[i][0])
if minfo['Monitor'][2] > SCREEN_HEIGHT:
SCREEN_HEIGHT = minfo['Monitor'][3]
print("Monitor #" + str(i+1) + ": " + str(minfo['Monitor'][2] - curr_right) + "x" + str(minfo['Monitor'][3]))
i += 1
curr_right += minfo['Monitor'][2]
minfo = win32api.GetMonitorInfo(mons[i-1][0])
SCREEN_WIDTH = minfo['Monitor'][2]
XMAX = SCREEN_WIDTH - 1
YMAX = SCREEN_HEIGHT - 1
hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.SubscribeMouseMove(trackMouse)
hm.KeyDown = onKeyboardEvent
hm.HookMouse()
hm.HookKeyboard()
pythoncom.PumpMessages()
代码是有效的,但无论我做了什么尝试,我似乎都无法避免光标反弹到边界,而不是跳到另一边。我已经没有主意了,所以我想也许这里有人会想一想发生了什么
提前感谢您提供的任何见解
更新:我创建了一个名为wrap.py的鼠标环绕python脚本,并将结果放在GitHub上的https://github.com/XyzzyBob/wrap。我也在Win10下使用pyinstaller编译了它,但是无法上传文件,因为我无法从代码所在的位置传输文件
正如github页面上所指出的,python脚本需要一组库,并且高度依赖于Windows。如果我觉得有必要的话,我可能会在某个时候为Linux和Mac重做它,但现在Windows是我使用它的地方
仍在为多系统控制开发网络版本,但这是一个更大的项目。这方面的进展可能会在github页面上显示为一个分支
==========================================XXXXXXXXXXXXXXXXXXXX====================================
找到了一种方法,通过使用不同的库来实现我想要做的事情。鼠标库显然处理低级钩子,并具有获取和设置鼠标状态项(如位置)的例程。解决方案基本上如下所示:
现在只需添加键盘挂钩和其他部分,使其成为一个连贯的包
相关问题 更多 >
编程相关推荐