Respond to the left mouse click button in GUI

17 views (last 30 days)
Hi everyone,
I'm designing a GUI which having several editboxs (allow input), and a figure_axis(explanation figure corresponding to the clicked editbox). I want to it to display my assigned explanation figure when I click (left click) to editboxs. Is there any way to do this?
Thank you so much!

Accepted Answer

Joseph Cheng
Joseph Cheng on 4 Sep 2014
there is a problem with what you're trying to accomplish. here is a quick demo you can run. copy and paste into a mfile called clicktest. Here you can see the text field on the lower right updates the X and Y location of a mouse click but doesn't update when you click on a GUI interface (button, either edit boxes, and even the text field).
function clicktest()
fig = figure
hax = axes('Units','pixels');
surf(peaks)
uicontrol('Style', 'pushbutton', 'String', 'Clear',...
'Position', [20 20 50 20],...
'Callback', 'surf(randi(5,49,49)*peaks)');
% The pushbutton string callback
% calls a MATLAB function
ustring = uicontrol('Style','text',...
'Position',[400 45 120 20],...
'String','clicked objectt')
edit1= uicontrol('Style','edit',...
'Position',[400 95 120 20],...
'String','clicked object')
edit2= uicontrol('Style','edit',...
'Position',[400 115 120 20],...
'String','clicked object')
set(fig,'WindowButtonDownFcn',{@position,ustring});
end
function position(hobject,event,ustring)
C = get(hobject,'CurrentPoint');
set(ustring,'String',num2str(C));
end
I would go with a radio button group that you put next to each text box.

More Answers (1)

Geoff Hayes
Geoff Hayes on 4 Sep 2014
It is possible to capture left-mouse button events around an edit box, but it is a little clumsy, and like Joseph indicated, it won't work if you click within the edit box, just around it.
function captureMouseClick
% create the figure
h = figure;
% create the edit widget
edit1h = uicontrol('Style', 'edit',...
'String', 'enter text here',...
'Position', [20 340 100 50],...
'ButtonDownFcn', @edit1ButtonDown);
% the callback to capture the mouse button down event for the edit widget
function edit1ButtonDown(hObject,~)
% get the last mouse click selection type within the figure
seltype = get(h,'SelectionType');
if strcmpi(seltype,'normal')
fprintf('left mouse button pressed!\n');
elseif strcmpi(seltype,'alt')
fprintf('right mouse button pressed!\n');
elseif strcmpi(seltype,'open')
fprintf('double mouse click!\n');
else
fprintf('other - shift and mouse-click!\n');
end
end
end
Seems only to work if you are within a few pixels outside the border of the edit widget. Try it and see what happens!

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!