Page 1 of 1

Detecting mouse click out of a window

PostPosted: Mon Jan 23, 2006 7:31 pm
by Rafael Clemente
Is there any function to detect LClick of RClick on any place of the screen?. What I am trying to do is to find a function that returns the cursor absolute coordinates (with GetCursorPos()) when I press LClick(), irrespective of any window.

Thanks
Rafael

PostPosted: Tue Jan 24, 2006 1:02 am
by Antonio Linares
Rafael,

There are two ways to do it:

1. You may capture the mouse, so all the mouse events get routed to your window, until you release the mouse capture.

2. You may use a timer to call GetCursorPos() to know the mouse position on a certain time interval.

PostPosted: Tue Jan 24, 2006 7:59 am
by Rafael Clemente
Antonio:

1.- Yes, but how can I capture the mouse all over the desktop?. I want to click anywhere in the screen and get its coords. For example:

hWnd := GetDesktopWindow() // Handle to the desktop
oWnd:bLClicked := {|r,c| MsgInfo("Clicked at "+STR(x)+STR(y)}

But how can I get oWnd from hWnd? I tried oWnd := oWndFromhWnd(hWnd) but it returns NIL


2.- GetCursorPos() works fine. The problem is capturing the click.

Rafael

PostPosted: Tue Jan 24, 2006 9:50 am
by Antonio Linares
Rafael,

To capture the mouse you use SetCapture():

The SetCapture function sets the mouse capture to the specified window belonging to the current thread. Once a window has captured the mouse, all mouse input is directed to that window, regardless of whether the cursor is within the borders of that window. Only one window at a time can capture the mouse.

You may do:

Code: Select all  Expand view
oWnd:Capture()
oWnd:bLClicked := {|r,c| MsgInfo("Clicked at "+STR(x)+STR(y)}


oWnd is your window. Not the desktop window.

From that moment on, any click, anywhere on the screen, will be routed to your oWnd:bLClicked codeblock. Same for bLButtonUp, bMButtonDown, bMButtonUp, bRClicked, bRButtonUp, bLDblClick.

To release the capture, you call ReleaseCapture() (no parameters).

The coordinates you will get are relative to the window that has captured the mouse, so if you want to locate another window from a click then you have to use WindowFromPoint( nRow, nCol ) --> hWnd. To locate a specific child control use ChildWindowFromPoint( hWndParent, aPoint ) where aPoint is { nRow, nCol }.