Generating Mouse-Keyboard combination events in python

Posted by freakazo on Stack Overflow See other posts from Stack Overflow or by freakazo
Published on 2010-04-23T23:50:44Z Indexed on 2010/04/23 23:53 UTC
Read the original article Hit count: 254

I want to be able to do a combination of keypresses and mouseclicks simultaneously, as in for example Control+LeftClick

At the moment I am able to do Control and then a left click with the following code:

import win32com, win32api, win32con
def CopyBox( x, y):
    time.sleep(.2)
    wsh = win32com.client.Dispatch("WScript.Shell")
    wsh.SendKeys("^")
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)

What this does is press control on the keyboard, then it clicks. I need it to keep the controll pressed longer and return while it's still pressed to continue running the code. Is there a maybe lower level way of saying press the key and then later in the code tell it to lift up the key such as like what the mouse is doing?

© Stack Overflow or respective owner

Related posts about mouseevent

Related posts about keyboard-hook