简体   繁体   中英

win32api move mouse virtually

I'm trying to simulate a mouseclick with python

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.

See Multiple mouse/mice/cursor? , which refers to Windows MultiPoint Mouse SDK . The CPNMouse project no longer exists, AFAICT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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