Link to home
Start Free TrialLog in
Avatar of yingkit
yingkit

asked on

Simulate clicks/double clicks globally?

Hi,
How can I simulate single clicks and double clicks GLOBALLY(ie. anywhere in the screen)?
I want to do this only with the built-in components in Delphi.
(D2/Win95/Sample code expected)
Avatar of WiseGuy
WiseGuy

So you want to sent a windows message but not to a specific window handle?
Avatar of yingkit

ASKER

What I want is just to send single/double clicks to any specified x,y coordinates on the screen...
You would have to find out the windows message that decodes this.

Say this message is <M>.
Then you call this windows api method:

PostMessage(HWND_BROADCAST,M,X,Y)

it might be the other way around for the X and Y coordinates

PostMessage(HWND_BROADCAST,M,Y,X)


See the Win32.hlp file in program files\common files\borland shared\mshelp
Avatar of yingkit

ASKER

Sample code?
working on it.
 
Avatar of yingkit

ASKER

Thx
Well, I see 3 possibilites:

(1) WiseGuy's suggestion.
(2) Use WindowFromPoint to get the window handle at the position, where you want to simulate the clicks. Then call PostMessage(window, WM_LBUTTONDOWN, ...) and PostMessage(window, WM_LBUTTONUP, ...).
(3) Use SetCursorPos. Then call Mouse_Event. This should work best. But the mouse is really moving to the simulated position.

Regards, Madshi.
Well,

BroadCast doesn't work because the first window that accepts it takes the message from the queue. This window is hardly ever the one intended. The desktop is the most likely one.
So you would have to go with Madshi's option 2 or 3.

If I supply you with sample code, would you both consider that an answer
Yes...   :-)   Post an answer...
as soon as I got it working.

It turns out that only Mouse_Event works.
I call it, indirectly from a button.click.
This makes this click trigger all of the time.
I'm trying to solve this with a timer, so that the click event is finished before a new mouse event takes place.
why dont you use mouse_event API?

to simulate single clicks :
mouse_event()
  mouse_event(MOUSEEVENTF_LEFTDOWN,x,y,0,0);
  mouse_event(MOUSEEVENTF_LEFTUP,x,y,0,0);

and double clicks is
  mouse_event(MOUSEEVENTF_LEFTDOWN,x,y,0,0);
  mouse_event(MOUSEEVENTF_LEFTUP,x,y,0,0);
  mouse_event(MOUSEEVENTF_LEFTDOWN,x,y,0,0);
  mouse_event(MOUSEEVENTF_LEFTUP,x,y,0,0);

(x,y) is your mousepoint!
ASKER CERTIFIED SOLUTION
Avatar of WiseGuy
WiseGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yingkit

ASKER

EXCELLENT!
Thank you very much.
yingkit,

what did you grade me?
Avatar of yingkit

ASKER

An "A" is given.
Thanx,

I couldn't find what grade was given to me for what answer.


Yes, I already have answered more then one question

CU, Ramon
WiseGuy, you can see that from the points you got. This q. was worth 40 points. If you got 4*40, you got an A grade. If you got 3*40 = B. 2*40 = C. 1*40 = D.
I see,

I had noticed that I got more points than offered, but I thought it was related to the amount of experts participating in the discussion.

Thank you for salvaging me from my erronous way. :)