Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Query if a mouse button is down
Replies: 8   Last Post: Jan 20, 2014 2:34 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Jim Hokanson

Posts: 14
Registered: 1/27/11
Re: Query if a mouse button is down
Posted: Apr 6, 2012 3:19 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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.

Jim



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.