简体   繁体   English

win32api虚拟移动鼠标

[英]win32api move mouse virtually

I'm trying to simulate a mouseclick with python 我正在尝试使用python模拟mouseclick

My code is the following: 我的代码如下:

class Mouse(object):

    def __init__(self):
        self.x_res = win32api.GetSystemMetrics(0)
        self.y_res = win32api.GetSystemMetrics(1)

    def rightClick(self, x, y):
        nx = x*65535/self.x_res
        ny = y*65535/self.y_res
        win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_MOVE,nx,ny)
        win32api.Sleep(200)
        win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)
        win32api.Sleep(200)
        win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE|win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)
        win32api.Sleep(700)

I use the code like this: 我使用这样的代码:

mouse = Mouse()
win32api.Sleep(2000)
x = 2520
y = 414
mouse.leftClick(x, y)

However this moves the real mouse, so it is disturbing the user. 但是,这会移动真正的鼠标,因此会打扰用户。 Is they a way to send that event but without moving the mouse for real ? 它们是否可以发送事件但不移动鼠标来实现?

If you insist on faking input to automate this action, then you are stuck with you current problem. 如果您坚持使用伪造的输入来自动执行此操作,那么您将陷入当前的困境。 When you fake input, the system will respond as it would for real input. 当您伪造输入时,系统将像实际输入一样做出响应。 You can the cursor back to its original location, but the user is going to see it flicking about. 您可以将光标移回其原始位置,但用户将看到它在弹动。 It's really not a good thing to do. 确实不是一件好事。

What you need is a solution to your problem that does not involve faking input. 您需要的是不涉及伪造输入的解决方案。 Of course, you did not elaborate the problem, only your solution, so we have to guess at the problem. 当然,您没有详细说明问题,仅是您的解决方案,因此我们必须猜测问题。 It looks like you are trying to automate some external application. 看来您正在尝试使某些外部应用程序自动化。 The platform provides support for that via UIAutomation. 该平台通过UIAutomation提供了对此的支持。

See Multiple mouse/mice/cursor? 看到多个鼠标/鼠标/光标? , which refers to Windows MultiPoint Mouse SDK . ,它指的是Windows MultiPoint Mouse SDK The CPNMouse project no longer exists, AFAICT. CPNMouse项目不再存在,AFAICT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM