I just came across this post when trying to do the same thing. On windows I've managed to use user32.dll to get it to work (uses function GetAsyncKeystate)
1) create file user32.h, should contain the following:
short GetAsyncKeyState(int vKey);
EXTRA NOTES (not necessary to read) (see documentation on microsoft website, I did a Google search for GetAsyncKeystate) NOTE: The function returns a SHORT, not short, which I assume allows them to define what a short should be on the user's system during compilation, I tried the following and it didn't work. I'm not sure why ...
typedef short SHORT SHORT GetAsyncKeyState(int vKey);
Also, there is a leading __in in the definition which I think is defined as nothing but allows some documentation to work so I omitted it
2) Run the function: loadlibrary('user32.dll','user32.h')
NOTE: A mex compiler should be setup to get this to work. Use: mex -setup to get one setup NOTE: You can check if the library is loaded in your code by using the following call: libisloaded('user32')
3) The keys are defined on Microsoft's website. The left mouse button is 1, right mouse button is 2. After creating the small user32.h file and running the function, this following test function works. I put it in a loop to observe it in action. Press and release the mouse button during the loop to see it in action.
tic for i = 1:100 disp(calllib('user32','GetAsyncKeyState',int32(1))) pause(0.2) end toc
NOTE: The input must be an int32.
In actual use, I'll probably load the library on startup and write a wrapper to the library call. Hope that helps.