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:
Problem with button callbacks on mouse down

Subject: Problem with button callbacks on mouse down

From: J Queue

Date: 10 Feb, 2010 19:06:05

Message: 1 of 2

A uicontrol pushbutton executes its callback when the mouse is RELEASED, rather than when it is PRESSED on the button. I need to have a button that executes the callback when the button is pressed.

I tried a bunch of stuff and basically ended up writing my own button using uipanel (see below). This works, except that if I place text on the button (using uicontrol text), the text captures the mouse click. I tried using 'HitTest', 'off' and also tried to set a callback, but neither approach works.

Any insight -- including another approach if you have one --- would be appreciated. Please post to the forum, as the e-mail address is temporary.

Thanks!

function mybutton
set(gcf, 'DefaultUiControlUnits', 'norm')
h = uipanel('Position', [0.1 0.1 0.1 0.1], ...
'BorderType', 'beveledout', ...
'ButtonDownFcn', @down)

% This control will capture mouse clicks
% I tried 'HitTest', 'off' and also 'Callback' @down
% but neither worked
uicontrol('Style', 'text', ...
'Parent', h, ...
'Position', [0.2 0.2 0.5 0.5], ...
'String', '1', ...
'FontSize', 12, ...
'FontWeight', 'bold');

function down(src, evt)
set(h, 'BorderType', 'beveledin')
set(gcf, 'WindowButtonUpFcn', @up);
disp('*')
end

function up(src, evt)
set(h, 'BorderType', 'beveledout')
set(gcf, 'WindowButtonUpFcn', '');
end
end

Subject: Problem with button callbacks on mouse down

From: Yair Altman

Date: 10 Feb, 2010 23:15:06

Message: 2 of 2

"J Queue" <b3764718@uggsrock.com> wrote...
> A uicontrol pushbutton executes its callback when the mouse is RELEASED, rather than when it is PRESSED on the button. I need to have a button that executes the callback when the button is pressed.


This is possible using the uicontrol's underlying Java object, as explained here: http://undocumentedmatlab.com/blog/uicontrol-callbacks/

Simply download the FindJObj utility from the File Exchange ( http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects ) and then:

hControl = uicontrol(...);
jControl = findjobj(hControl);
set(jControl,'KeyPressedCallback',@myMatlabCallbackFunction);

The FindJObj utility is explained here: http://undocumentedmatlab.com/blog/findjobj-find-underlying-java-object/

Yair Altman
http://UndocumentedMatlab.com

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