Hi,
@jan: As I said, the java robot solution does not work in my application. I want to control the mouse in a video game. The game runs in full screen mode and somehow it overrides the java robot.
Anyway, the solution I came up with is this: First create a mex file, include "windows.h" and "winbase.h" Not sure if you really need both. They don't have to be in the Matlab working directory. Matlab should find them in windows/system.
Next pass some numbers inside the mex to the function SetCursorPos(x,y). X and Y should be int32 if I recall correctly. This positions the mouse at the pixel coordinate (x,y) on the screen. I used get(0, 'ScreenSize') in the m-file that calls the mex file because the video game changed the screen resolution on start up.
Last I used the process thread to simulate a mouse click. Inside the mex file I wrote: PostMessage(GetForegroundWindow(),WM_LBUTTONDOWN,0,0); Sleep(20); PostMessage(GetForegroundWindow(),WM_LBUTTONUP,0,0); Sleep(20);
If I understand correctly this posts the code for a mouse click in the message queue of the active window. I used postmessage, because I didn't want to wait for it to actually do so. I you want to wait until the message is actually posted, use sendmessage instead. WM_LBUTTONDOWN and UP are already defined in the header files so they are automatically the right type. GetForegroundWindow() is a pointer the the active window, the application inside which I wanted to click the mouse.
Compile the mex-file and call it from an m-file or from the workspace. The postmessage and sendmessage functions allow you to send anything to any process. It does not even need to be in focus. Just create a pointer to the application you want to control.