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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
OK, first you have to know how to open the user32 windll via
ctypes, which is trivial:Next, the Win32 function you want to call is probably SendInput, although you might want to look at
mouse_eventand possiblySendMessage(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 oneMOUSEEVENTF_LEFTDOWNfollowed by oneMOUSEEVENTF_LEFTUP, with 0 for all of the params besidesdwFlags.So, how do you call this? Well, here’s the C API:
That
LPINPUTmeans you’ve got a pointer to an array ofINPUTstructures. Since theINPUTstructure itself has a union ofMOUSEINPUT,KBDINPUT, andHARDWAREINPUT, 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
ctypesare:Structures forMOUSEINPUTandINPUT.user32windll.argtypesforuser32.SendInput.MOUSEINPUTinstance, withdwFlags= MOUSEEVENTF_LEFTDOWN, and theINPUTinstance to go with it.user32.SendInput(1, [myinput], len(INPUT))oruser32.SendInput(1, addressof(myinput), len(INPUT))or whatever (depending on how you defined theargtypes).MOUSEINPUTinstance, withdwFlags= MOUSEEVENTF_LEFT, and theINPUTinstance to go with it.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?