Connect with Expertise | Find Experts, Get Answers, Share Insights

Mouseclick

Terje Johan Abrahamsen
 
Posts: n/a
#1: Jul 19 '05
Hello.

I have been trying desperately for a while to make Python push the
left mousebutton. I have been able to let Python push a button in a
box:

def click(hwnd):
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)

optDialog = findTopWindow(wantedText="Options")

def findAButtonCalledOK(hwnd, windowText, windowClass):
return windowClass == "Button" and windowText == "OK"
okButton = findControl(optDialog, findAButtonCalledOK)

click(okButton)

As described here, http://tinyurl.com/cwjls. But, that is not what I
am looking for. I would like to specify some coordinates such as
windll.user32.SetCursorPos(450, 370) and thereafter click the left
mousebutton at that place.

I know that the sollution lies somewhere with Microsoft
(http://www.6URL.com/FED), but cannot understand how to make Python
click the button regardless of how much I try.

Thanks in advance.

Benji York
 
Posts: n/a
#2: Jul 19 '05

re: Mouseclick


Terje Johan Abrahamsen wrote:[color=blue]
> I have been trying desperately for a while to make Python push the
> left mousebutton.[/color]

Here's some code I've used to simulate a *right* click, but it should
be obvious how to make it do what you want:

void sendRightClick(void)
{
PostMessage(GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON, 0);
PostMessage(GetForegroundWindow(), WM_RBUTTONUP, 0, 0);
}

--
Benji York
terjeja@gmail.com
 
Posts: n/a
#3: Jul 19 '05

re: Mouseclick



Benji York wrote:[color=blue]
> Terje Johan Abrahamsen wrote:[color=green]
> > I have been trying desperately for a while to make Python push the
> > left mousebutton.[/color]
>
> Here's some code I've used to simulate a *right* click, but it should[/color]
[color=blue]
> be obvious how to make it do what you want:
>
> void sendRightClick(void)
> {
> PostMessage(GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON,[/color]
0);[color=blue]
> PostMessage(GetForegroundWindow(), WM_RBUTTONUP, 0, 0);
> }[/color]

Thanks for the input. I have 'Pythonized' the code a little:
[color=blue][color=green][color=darkred]
>>> def click():[/color][/color][/color]
.... windll.user32.SetCursorPos(100, 100)
.... win32gui.PostMessage(win32gui.GetActiveWindow(),
win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, 0)
.... win32gui.PostMessage(win32gui.GetActiveWindow(),
win32con.WM_LBUTTONUP, 0, 0)

and when I try it like this:

n = click()

Sure, the cursor moves, but it does not click. What am I doing wrong?

Thanks in advance

terjeja@gmail.com
 
Posts: n/a
#4: Jul 19 '05

re: Mouseclick



Benji York wrote:[color=blue]
> Terje Johan Abrahamsen wrote:[color=green]
> > I have been trying desperately for a while to make Python push the
> > left mousebutton.[/color]
>
> Here's some code I've used to simulate a *right* click, but it should[/color]
[color=blue]
> be obvious how to make it do what you want:
>
> void sendRightClick(void)
> {
> PostMessage(GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON,[/color]
0);[color=blue]
> PostMessage(GetForegroundWindow(), WM_RBUTTONUP, 0, 0);
> }[/color]

Thanks for the input. I have 'Pythonized' the code a little:
[color=blue][color=green][color=darkred]
>>> def click():[/color][/color][/color]
.... windll.user32.SetCursorPos(100, 100)
.... win32gui.PostMessage(win32gui.GetActiveWindow(),
win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, 0)
.... win32gui.PostMessage(win32gui.GetActiveWindow(),
win32con.WM_LBUTTONUP, 0, 0)

and when I try it like this:

n = click()

Sure, the cursor moves, but it does not click. What am I doing wrong?

Thanks in advance

snoe
 
Posts: n/a
#5: Jul 19 '05

re: Mouseclick


I did this a little while ago, there's some setup stuff in here for
sending keyboard commands as well. Coercing the structs into python was
the hardest part. Basically Click(x,y) moves the mouse to the specified
spot on the screen (not window) sends a mouse down followed by mouse up
event then returns to your original position. Sorry for lack of
comments.


from ctypes import *
import time


PUL = POINTER(c_ulong)
class KeyBdInput(Structure):
_fields_ = [("wVk", c_ushort),
("wScan", c_ushort),
("dwFlags", c_ulong),
("time", c_ulong),
("dwExtraInfo", PUL)]

class HardwareInput(Structure):
_fields_ = [("uMsg", c_ulong),
("wParamL", c_short),
("wParamH", c_ushort)]

class MouseInput(Structure):
_fields_ = [("dx", c_long),
("dy", c_long),
("mouseData", c_ulong),
("dwFlags", c_ulong),
("time",c_ulong),
("dwExtraInfo", PUL)]

class Input_I(Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]

class Input(Structure):
_fields_ = [("type", c_ulong),
("ii", Input_I)]

class POINT(Structure):
_fields_ = [("x", c_ulong),
("y", c_ulong)]

def Click(x,y):

orig = POINT()

windll.user32.GetCursorPos(byref(orig))

windll.user32.SetCursorPos(x,y)

FInputs = Input * 2
extra = c_ulong(0)

ii_ = Input_I()
ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )

ii2_ = Input_I()
ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )

x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )

windll.user32.SendInput(2, pointer(x), sizeof(x[0]))

return orig.x, orig.y

M.E.Farmer
 
Posts: n/a
#6: Jul 19 '05

re: Mouseclick


This could be fun!
# random clicks
# play with values to maximize annoyance
if __name__ == "__main__":
import random
for i in range(1000):
x = random.randint(0,800)
y = random.randint(0,600)
Click(x,y)
time.sleep(random.randint(0,20))

garyr@fidalgo.net
 
Posts: n/a
#7: Jul 19 '05

re: Mouseclick



Terje Johan Abrahamsen wrote:[color=blue]
> Hello.
>
> I have been trying desperately for a while to make Python push the
> left mousebutton. I have been able to let Python push a button in a
> box:
>
> def click(hwnd):
> win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
> win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)
>
> optDialog = findTopWindow(wantedText="Options")
>
> def findAButtonCalledOK(hwnd, windowText, windowClass):
> return windowClass == "Button" and windowText == "OK"
> okButton = findControl(optDialog, findAButtonCalledOK)
>
> click(okButton)
>
> As described here, http://tinyurl.com/cwjls. But, that is not what I
> am looking for. I would like to specify some coordinates such as
> windll.user32.SetCursorPos(450, 370) and thereafter click the left
> mousebutton at that place.
>
> I know that the sollution lies somewhere with Microsoft
> (http://www.6URL.com/FED), but cannot understand how to make Python
> click the button regardless of how much I try.
>
> Thanks in advance.[/color]


Another, perhaps not so cool, way of doing this is to just invoke the
mouse handler functions directly. e.g.:

class Event:
def __init__(self, x, y):
self.x = x
self.y = y

class Whatever:
Closed Thread