Is there a way to invoke a leftclick in python?
Question: I only have the python 2.6. Can I do this without using external libraries? I
just want to perform a left click wherever the cursor currently is.


Answer: OK, first you have to know how to open the user32 windll via `ctypes`, which
is [trivial](http://docs.python.org/2/library/ctypes.html#loading-dynamic-
link-libraries):

    
    
    from ctypes import *
    user32 = windll.user32
    

Next, the Win32 function you want to call is probably
[SendInput](http://msdn.microsoft.com/en-
us/library/windows/desktop/ms646310%28v=vs.85%29.aspx), although you might
want to look at `mouse_event` and possibly `SendMessage` (and the
documentation for which WM_* messages correspond to a mouse click) to compare
and contrast.

Assuming you go with `SendInput`, you're going to send one
`MOUSEEVENTF_LEFTDOWN` followed by one `MOUSEEVENTF_LEFTUP`, with 0 for all of
the params besides `dwFlags`.

So, how do you call this? Well, here's the C API:

    
    
    UINT WINAPI SendInput(
      _In_  UINT nInputs,
      _In_  LPINPUT pInputs,
      _In_  int cbSize
    );
    

That `LPINPUT` means you've got a pointer to an array of
[`INPUT`](http://msdn.microsoft.com/en-
us/library/windows/desktop/ms646270%28v=vs.85%29.aspx) structures. Since the
`INPUT` structure itself has a union of `MOUSEINPUT`, `KBDINPUT`, and
`HARDWAREINPUT`, you'll also need to define those (although you can get away
with just defining the first and pretending the others don't exist, since the
first is the only one you need).

So, the steps to doing this with `ctypes` are:

  * Define the [`Structure`](http://docs.python.org/2/library/ctypes.html#structures-and-unions)s for [`MOUSEINPUT`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273%28v=vs.85%29.aspx) and [`INPUT`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx).
  * Load the `user32` windll.
  * Set the [`argtypes`](http://docs.python.org/2/library/ctypes.html#specifying-the-required-argument-types-function-prototypes) for `user32.SendInput`.
  * Create your first `MOUSEINPUT` instance, with `dwFlags= MOUSEEVENTF_LEFTDOWN`, and the `INPUT` instance to go with it.
  * Call `user32.SendInput(1, [myinput], len(INPUT))` or `user32.SendInput(1, addressof(myinput), len(INPUT))` or whatever (depending on how you defined the `argtypes`).
  * Create your second `MOUSEINPUT` instance, with `dwFlags= MOUSEEVENTF_LEFT`, and the `INPUT` instance to go with it.
  * Call `user32.SendInput()` again.

One last note: On Vista and above, you have to have the right "integrity
level" to do this. For example, a normal app cannot send a click to an
"elevated" app (e.g., an installer). Hopefully this isn't an issue for you. If
it is, you need to read up on UIPI and UAC… and if worst comes to worst, you
may need to fall back to lower-level tricks.

Is that enough, or do you need help with some specific part of this?

