Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Mouseclick

Reply
Thread Tools

Mouseclick

 
 
Terje Johan Abrahamsen
Guest
Posts: n/a
 
      05-02-2005
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.
 
Reply With Quote
 
 
 
 
terjeja@gmail.com
Guest
Posts: n/a
 
      05-02-2005

Benji York wrote:
> Terje Johan Abrahamsen wrote:
> > I have been trying desperately for a while to make Python push the
> > left mousebutton.

>
> 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);
> }


Thanks for the input. I have 'Pythonized' the code a little:

>>> def click():

.... 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

 
Reply With Quote
 
 
 
 
terjeja@gmail.com
Guest
Posts: n/a
 
      05-02-2005

Benji York wrote:
> Terje Johan Abrahamsen wrote:
> > I have been trying desperately for a while to make Python push the
> > left mousebutton.

>
> 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);
> }


Thanks for the input. I have 'Pythonized' the code a little:

>>> def click():

.... 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

 
Reply With Quote
 
Benji York
Guest
Posts: n/a
 
      05-02-2005
Terje Johan Abrahamsen wrote:
> I have been trying desperately for a while to make Python push the
> left mousebutton.


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
 
Reply With Quote
 
snoe
Guest
Posts: n/a
 
      05-02-2005
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

 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      05-02-2005
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))

 
Reply With Quote
 
garyr@fidalgo.net
Guest
Posts: n/a
 
      05-03-2005

Terje Johan Abrahamsen wrote:
> 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.



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:
.
.
def mouseClick(self, event):
self.mouseDown(event) # link to ButtonPress handler
self.mouseUp(event) # link to ButtonRelease handler

...
event.x, event.y = 123, 456
self.mouseClick(event)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching MouseClick events jediknight ASP .Net 2 07-04-2006 03:01 PM
[JNI] Catch Ctrl+Mouseclick on Windows? watzlaw wutz Java 2 05-11-2004 06:16 PM
RE: winapi: mouseclick Tim Golden Python 1 01-27-2004 04:52 PM
RE: winapi: mouseclick Tim Golden Python 0 01-27-2004 09:23 AM
winapi: mouseclick KNS Python 1 01-27-2004 08:19 AM



Advertisments