Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Query if a mouse button is down

Subject: Query if a mouse button is down

From: Oliver Woodford

Date: 15 Feb, 2011 15:32:05

Message: 1 of 9

Hi all

I need a function that will tell me whether a particular mouse button is up or down at the moment the function is called. I don't want it to use callbacks in the background. Can anyone help?

Thanks,
Oliver

Subject: Query if a mouse button is down

From: Oliver Woodford

Date: 15 Feb, 2011 16:14:04

Message: 2 of 9

"Oliver Woodford" wrote:
> I need a function that will tell me whether a particular mouse button is up or down at the moment the function is called. I don't want it to use callbacks in the background. Can anyone help?

"At the moment ..." is probably overly complicated. The state of the button at the last drawnow or callback is fine. I just don't wan't to be faffing with boolean variables and button up and down callbacks.

Subject: Query if a mouse button is down

From: Paulo Silva

Date: 15 Feb, 2011 16:43:04

Message: 3 of 9

http://www.mathworks.com/matlabcentral/newsreader/view_thread/268250

Subject: Query if a mouse button is down

From: Oliver Woodford

Date: 15 Feb, 2011 17:19:06

Message: 4 of 9

"Paulo Silva" wrote:
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/268250

Thanks, Paulo. I'd seen that thread (and wondered whether someone would point to it). However, it doesn't suit my needs. I want to know the state of a mouse button *now* (i.e. when the function is called, from wherever), and not only when the ButtonDownFcn callback is called.

I can of course set up a system of callbacks to set and unset a flag, then query this flag, but I'd like to avoid this. Why? Because the callbacks have several options, such as Interruptible and BusyAction, which may affect the behaviour, indeed the integrity of this method. There might be bugs, and I don't want to spend my time discovering and fixing them, especially when I might change these settings later.

Subject: Query if a mouse button is down

From: John Marcovici

Date: 14 Jul, 2011 00:04:10

Message: 5 of 9

Did you get to the bottom of this? I'm in the same situation where I'd like to know the mouse state at any time, and don't want to set a flag.

"Oliver Woodford" wrote in message <ijecia$e7c$1@fred.mathworks.com>...
> "Paulo Silva" wrote:
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/268250
>
> Thanks, Paulo. I'd seen that thread (and wondered whether someone would point to it). However, it doesn't suit my needs. I want to know the state of a mouse button *now* (i.e. when the function is called, from wherever), and not only when the ButtonDownFcn callback is called.
>
> I can of course set up a system of callbacks to set and unset a flag, then query this flag, but I'd like to avoid this. Why? Because the callbacks have several options, such as Interruptible and BusyAction, which may affect the behaviour, indeed the integrity of this method. There might be bugs, and I don't want to spend my time discovering and fixing them, especially when I might change these settings later.

Subject: Query if a mouse button is down

From: Oliver Woodford

Date: 2 Aug, 2011 08:21:09

Message: 6 of 9

"John Marcovici" wrote:
> Did you get to the bottom of this? I'm in the same situation where I'd like to know the mouse state at any time, and don't want to set a flag.

No, I didn't. I ended up going for the callbacks setting flags.

Subject: Query if a mouse button is down

From: Jim Hokanson

Date: 6 Apr, 2012 19:19:12

Message: 7 of 9

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

Subject: Query if a mouse button is down

From: John

Date: 7 Apr, 2012 02:29:36

Message: 8 of 9

I did something similar Jim, based on what I found here:

http://vcpptips.wordpress.com/tag/check-if-mouse-is-pressed

but ended up not going with it, because I needed to support windows and linux. I ended up doing what everyone thinks of, and tries to avoid: when the button down function is called, set a global flag to true, and the button up sets it to false. Not super clean, but at least portable.

function button_down( obj )

global bd;

if strcmp( get( obj, 'selectiontype' ), 'normal' )
    bd = true;
end

Subject: Query if a mouse button is down

From: Benjamin

Date: 20 Jan, 2014 19:34:08

Message: 9 of 9

Jim,

I just started using the method you posted for some special resize functions and it works great. I'll only be running my code on Windows so its perfect for me.

Thanks!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us