MATLAB: How to determine the coordinates of mouse pointer after a mouse click event.

java robot mouse click event pointerlocationMATLAB

I have written a rather rudimentary function to automate an external software GUI by use of the java robot
Example:
robot.mouseMove(coordinates(1,1),coordinates(1,2));
robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
The software that I am using this function to automate is antiquated and requires the user to click through several settings and tabs within the GUI each time he/she wants to run the software. The function works wonderfully on my computer without any hiccups but I know that my mouseMove locations are unique to my monitor and will differ for another computer.
I am looking for a way to capture the location of the mouse pointer by using mouse clicks as an event (i.e., user clicks the mouse and Matlab retains the pixel coordinates). This way I can create a "setup file" for each user to run the first time they run the function. I currently am using coordinates(n,1:2)=get(0,’PointerLocation’) but I need to know if there is a way to tie this to a mouse click event.
An idea of what I am looking for:
n=0
MouseClickEvent toggles below code:
n=n+1;
coordinates(n,1:2)=get(0,PointerLocation)
Any advice is appreciated and I thank you in advance!

Best Answer

You can use FEX: WindowAPI to create a visible or almost transparent full-screen figure. Using an invisible or completely transparent window will most likely not work, because it does not catch mouse events.
Then the WindowButtonDownFcn can trigger the recording of the mouse position.
A more reliable method would be to get the screen position of the foreign GUI and set the mouse position relatively. This will work in a multi-monitor setup also, and the GUI can be moved freely by the user. Depending on the operating system there are different method to get the screen position of windows of other programs.
[EDITED] An example code taken from the source of WindowAPI.c:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
HWND hWnd;
RECT Win_Rect, Mon_Rect;
HMONITOR hMonitor;
MONITORINFO mInfo;
double *p;
// Set name of the window accordingly:
hWnd = FindWindow(NULL, "Name of the Window");
//
GetWindowRect(hWnd, &Win_Rect); // Window dimensions
hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
mInfo.cbSize = sizeof(mInfo);
GetMonitorInfo(hMonitor, &mInfo);
Mon_Rect = mInfo.rcMonitor; // Monitor dimensions
//
// Create output:
plhs[0] = mxCreateDoubleMatrix(1, 4, mxREAL);
p = mxGetPr(plhs[0]);
p[0] = Win_Rect.left - Mon_Rect.left + 1; // X from the left
p[1] = Mon_Rect.bottom - Win_Rect.bottom + 1; // Y measured from bottom
p[2] = Win_Rect.right - Win_Rect.left; // Width
p[3] = Win_Rect.bottom - Win_Rect.top; // Height
//
return;
}
UNTESTED: I do not have a Matlab or compiler currently, such that this is written from the scratch only.