473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Mouseclick

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.
Jul 19 '05 #1
6 6318
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
Jul 19 '05 #2

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

Jul 19 '05 #3

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

Jul 19 '05 #4
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

Jul 19 '05 #5
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))

Jul 19 '05 #6

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:
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vince | last post by:
Couldn't find a better place to post the question, feel free to direct me to a better place... Needing to automate our application testing using a keystroke/mouseclick recorder for playing back...
2
by: | last post by:
Hi everybody. Need your help. How can I play sound (.wav-file / camera click) on mouseclick? TIA willy wuff ...
3
by: Kubik | last post by:
Hello! How to detect mouseclick anywhere on the form? There is a lot of controls on the form, so I don't want to add eventhandler for each of them. How can I do it in another way? Adam --...
3
by: Hans | last post by:
Hi! In my windowsformapplication I have a DataGrid where the rowheader is not visible, and readOnly = true. The grid is bound to a DataView where AllowEdit, AllowNew and AllowDelete is set to...
4
by: Peter Rilling | last post by:
What is the difference between the MouseClick and Click events?
4
by: Chris Dunaway | last post by:
I added a MouseClick event to a Button control. It responds properly to the Left click of the button, but not a middle click or right click. Is it possible to get a right click and/or middle...
6
by: Kristian Frost | last post by:
Hi, I'm trying to add, as you might guess, mouseclick listeners to the shapes I am drawing using the GDI+ commands in a similar sort of way as could be done with the old VB "shapes". Problem is,...
1
by: JimBob | last post by:
Ok so i added this line of code to a dynamic PictureBox. (i dim it p) AddHandler p.MouseClick, AddressOf Hand.Canvas_Click what it would do is change the pic on the picbox but every time you click...
2
by: GS | last post by:
how can I tell if it is right mouse click from left mouseclick? I would also to test if a key like control is being held down at the same time. what I would really like to is to capture control...
6
by: RolltheBall | last post by:
Hi there. I am trying to make a certain symbol to appear upon every mouseclick. However, the symbol is a cross and an arrow head. This is how I tried to do it. Private Sub Form1_Paint(ByVal...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.