The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Query if a mouse button is down
Replies: 9   Last Post: Mar 9, 2017 3:58 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:

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:

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.

for i = 1:100

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.


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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.