GUI object not responding to left click

13 views (last 30 days)
Arnav Barbaad
Arnav Barbaad on 5 Jul 2016
Edited: Geoff Hayes on 6 Jul 2016
I am trying to detect a left mouse click on a GUI object (specfically, edit and static Box) but it absolutely refuses to do it. I tried 4-5 different methods of coding presented here, but none of them works. Funny thing is, it recognises right click but no matter what I do, it won't recognize a left click. Either from the trackpad or a mouse connected to my Mac. It even recognises double click IF AND ONLY IF it is followed with a right click, then it prints both double click and right click simultaneously (it won't recognize double click on its own)
Here is the simplest way I tried to code (with many other ways) but all of them give the exact same output as mentioned above
seltype = get(gcf,'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
WHAT IS WRONG WITH MATLAB???? :'(

Answers (1)

Geoff Hayes
Geoff Hayes on 5 Jul 2016
Arnav - when I use the above code in the WindowButtonDownFcn callback for my figure (not the edit text box), then MATLAB has no problem in recognizing a left-click, right-click and double-click. On my MacBook, pressing the trackpad results in a left-click, pressing the ctrl button and the trackpad results in a right-click, and double-clicking the track pad is obvious. Note that you will still register a left- or right-mouse click prior to the double-click. This is expected behaviour and will let you know if you have double-clicked with left or right button.
I am running with R2014a and OS X 10.11.5.
  1 Comment
Arnav Barbaad
Arnav Barbaad on 6 Jul 2016
Edited: Geoff Hayes on 6 Jul 2016
Hello Geoff,
Thank you for the quick reply. I started MATLAB a week ago and your replies on other threads have helped me a lot :)
I am using R2016a-Student use on OS X 10.11.5 and can't get it to work.
However, the following code opens a blank figure which DOES recognize left, right and double click properly but I can't really make out what is happening here:-
if nargin == 0
fig = figure;
set(fig, 'buttondownfcn', 'hit_tester(get(gcf, ''selectiontype''))');
else
if nargin == 1
button = varargin(1);
disp(button);
end
end
How can I implement the above for static or edit textbox in GUI?
Also, I know that the code in my original question registers a right or left click before registering double click, but as I said, it recognizes a double LEFT click IF AND ONLY IF I follow it with a single right click. Weird :/

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!