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:
Button Up/Down Function

Subject: Button Up/Down Function

From: Joe H

Date: 16 May, 2007 20:11:46

Message: 1 of 9

I have a GUI where I would like to set a parameter to a particular
value when the user presses and holds a button and then restore the
parameter to its original value when they release the button. For
example, as long as the button is pressed and held, the parameter
value = 1. When the user releases the button, the parameter = 0.

I've tried using the buttondownfcn however this only works when I
click the border of the button. And the callback fcn appears to work
on the left mouse button up (e.g. when the button has been pressed
and released).

Any ideas how I can accomplish this? Thanks in advance.

Subject: Button Up/Down Function

From: Yair Altman

Date: 16 May, 2007 20:31:20

Message: 2 of 9

You can use the figure's WindowButtonUpFcn callback function.

Note that when a figure Mode Manager (like zoom or pan) is active, it
"hijacks" WindowButtonUpFcn for their own need. If this affects you,
here's an undocumented/unsupported hack to get you the same result
without being "hijacked":

javaFrame = get(hFig,'JavaFrame');
axisComponent = get(javaFrame,'AxisComponent');
axisComponent = handle(axisComponent,'CallbackProperties');
set(axisComponent,'MouseReleasedCallback',myCallback);

(Note: this hack only works on relatively new Matlab versions)

Yair Altman

Subject: Button Up/Down Function

From: Joe H

Date: 16 May, 2007 22:02:10

Message: 3 of 9

Hello Yair,
     Thanks for the quick reply. I was hoping to bug you for more
clarification. I have 2 buttons on a GUI I've developed. When I
press button 1, I'd like to make a variable, say A, equal to 1. When
I release button 1, I want to make A = 0. On button 2, when I click
the button, I would like A = -1, and when I release button 2, A = 0.
When I use the WindowButtonDownFcn, it seems to generalize to the
figure the buttons reside on and I can't seem to delegate different
ButtonDownFcn's for each button. Thanks in advance for further
clarification.

JH

 Yair Altman wrote:
>
>
> You can use the figure's WindowButtonUpFcn callback function.
>
> Note that when a figure Mode Manager (like zoom or pan) is active,
> it
> "hijacks" WindowButtonUpFcn for their own need. If this affects
> you,
> here's an undocumented/unsupported hack to get you the same result
> without being "hijacked":
>
> javaFrame = get(hFig,'JavaFrame');
> axisComponent = get(javaFrame,'AxisComponent');
> axisComponent = handle(axisComponent,'CallbackProperties');
> set(axisComponent,'MouseReleasedCallback',myCallback);
>
> (Note: this hack only works on relatively new Matlab versions)
>
> Yair Altman

Subject: Button Up/Down Function

From: Yair Altman

Date: 17 May, 2007 03:34:04

Message: 4 of 9

Java mouse events have the ability to determine exactly which mouse
button was clicked etc. Unfortunately, MathWorks did not see fit to
pass this information to users (unlike the case of key-press
callbacks, where meta-info is available). Instead, Matlab seems to
pass an empty eventData argument, which is a real pity. It stands to
reasons that there's a system call somewhere telling us which button
is currently pressed. If someone knows, please post a reply here.

Matlab uses the secondary button as a uicontextmenu trigger, so maybe
you can use this somehow. I'm not exactly sure how, but it's a start.

Yair Altman

Subject: Button Up/Down Function

From: Yair Altman

Date: 17 May, 2007 04:23:35

Message: 5 of 9

Java events convey meta-info about the exact button pressed etc.
Unfortunately, Matlab passes an empty eventData object for mouse
events, unlike key-press events which pass meta-info. There's
probably a system call somewhere that tells us which mouse button is
currently clicked - if anyone knows it then please reply here.

Alternately, you can try to use the fact that Matlab uses the
secondary button for uicontextmenu - maybe try to catch this.

Yair Altman

Subject: Button Up/Down Function

From: Dan Sternberg

Date: 17 May, 2007 09:53:24

Message: 6 of 9

Hi Yair,

Though the event data is empty, the information on which button was pressed
is available through the "SelectionType" property of the figure. Its values
will be "normal", "extend" or "alt" depending on which mouse button was
pressed.

-I hope this helps.
 Dan

---
Dan Sternberg
The MathWorks, Inc.

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message
news:ef572ae.3@webcrossing.raydaftYaTP...
> Java events convey meta-info about the exact button pressed etc.
> Unfortunately, Matlab passes an empty eventData object for mouse
> events, unlike key-press events which pass meta-info. There's
> probably a system call somewhere that tells us which mouse button is
> currently clicked - if anyone knows it then please reply here.
>
> Alternately, you can try to use the fact that Matlab uses the
> secondary button for uicontextmenu - maybe try to catch this.
>
> Yair Altman

Subject: Button Up/Down Function

From: mike

Date: 18 Feb, 2010 20:04:06

Message: 7 of 9

I use this to get button-down, button-up events working:

function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

disp('down')
set(hObject,'Value',1)

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

if get(handles.pushbutton1,'Value')==1 %add more conditions here to check which %button is pushed
    disp('up')
end
set(handles.pushbutton1,'Value',0)

Subject: Button Up/Down Function

From: mike

Date: 18 Feb, 2010 20:04:06

Message: 8 of 9

I use this to get button-down, button-up events working:

function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

disp('down')
set(hObject,'Value',1)

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

if get(handles.pushbutton1,'Value')==1 %add more conditions here to check which %button is pushed
    disp('up')
end
set(handles.pushbutton1,'Value',0)

Subject: Button Up/Down Function

From: Björn

Date: 1 Apr, 2013 08:28:08

Message: 9 of 9

"mike " <miked.damianakis@gmail.com> wrote in message <hlk6fm$gf3$1@fred.mathworks.com>...
> I use this to get button-down, button-up events working:
>
> function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
> % hObject handle to pushbutton1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
>
> disp('down')
> set(hObject,'Value',1)


But this ButtonDownFcn will only run if you manages to hit the boarder or if you set the Enable property to 'off'... and then the normal callback wont trigger.

I would like to have Button Down/up function without disabling all my buttons...

>
> % --- Executes on mouse press over figure background, over a disabled or
> % --- inactive control, or over an axes background.
> function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
> % hObject handle to figure1 (see GCBO)
> % eventdata reserved - to be defined in a future version of MATLAB
> % handles structure with handles and user data (see GUIDATA)
>
> if get(handles.pushbutton1,'Value')==1 %add more conditions here to check which %button is pushed
> disp('up')
> end
> set(handles.pushbutton1,'Value',0)

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