Mouseclick

  • Thread starter Terje Johan Abrahamsen
  • Start date
T

Terje Johan Abrahamsen

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

terjeja

Benji said:
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:
.... 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
 
T

terjeja

Benji said:
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:
.... 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
 
B

Benji York

Terje said:
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);
}
 
S

snoe

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

M.E.Farmer

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))
 
G

garyr

Terje said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top