Creating mouse and keyboard combination events in python

I want to be able to do key combination and mouseclicks at the same time, like for example Control+LeftClick

At the moment I can do a Control and then 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 is it - press the control button on the keyboard, then press. I need the controller to push more and return while still pushing in order to continue executing the code. Is there a lower level way of saying press a key and then in code tell it to raise the key, like the way a mouse does?

+2


a source to share


1 answer


to control push:

win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_EXTENDEDKEY, 0)

      

to let it go:



win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP, 0)

      

so your code will look like:

import win32api, win32con
def CopyBox(x, y):
    time.sleep(.2)
    win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_EXTENDEDKEY, 0)
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)

      

+2


a source







All Articles