MATLAB Answers

How do I capture both the up and down edges of the same mouse click?

3 views (last 30 days)
Mandeguz
Mandeguz on 5 Jun 2017
Edited: Mandeguz on 5 Jun 2017
I am building a GUI, and I have managed to capture the down click of a mouse with ButtonDownFcn, and the up click with the uicontrol's callback. Within my callback function, I toggle the Enable property between "On" and "Inactive".
When the uicontrol is enabled, the ButtonDownFcn does not work. When the uicontrol is inactive, the callback does not work. Therefore, if I start on inactive and then switch it to active during the ButtonDownFcn, then the release should allow me to use the callback. At least, that's what I had thought.
However, it seems the pushbutton requires both a click and a release, with the function being activated on the release. Therefore, whenever I click the button, the initial click runs the function but the first release does not. Likewise, the next click does not work but the next release does. This then repeats for as many clicks as I so choose.
I see two routes; find a way to force a second click whenever the ButtonDownFcn is implemented, or find some magical way to force either the callback or the ButtonDownFcn to run on both the click and release of the mouse, possibly by toggling the click signal itself (so that it switches its interpretation of clicks and releases every time it happens).
Here's my uicontrol:
S.Ltopminipb_fill = uicontrol(S.fig,'style','pushbutton','units','normalized',...
'position',[col_1 flooring(7.5,'tp') arr_w flb_h],'Tag','Scroll',...
'FontUnits','normalized','FontWeight','bold','FontName','Symbol',...
'string',char(9650),'BackgroundColor', (clr.*0.7),'callback',{@pb_call,S},'ButtonDownFcn',{@pb_call,S},'Enable','Inactive');
And here's my toggler code:
U = varargin{1};
U.Enable
if(strcmp(U.String,char(9650))||strcmp(U.String,char(9660)))
if strcmp(U.Enable,'inactive')
U.Enable = 'on';
elseif strcmp(U.Enable,'on')
U.Enable = 'inactive';
end
end
U.Enable
For a bonus, I'm going to capture the time between clicking and releasing next. I'm sure people have solved this problem, but where is a good place to get started?

Answers (1)

Mandeguz
Mandeguz on 5 Jun 2017
Edited: Mandeguz on 5 Jun 2017
Found a solution, but I'm open to better ones (I'm sure more elegant ones exist).
U = varargin{1};
U.Enable
if(strcmp(U.String,char(9650))||strcmp(U.String,char(9660)))
if strcmp(U.Enable,'inactive')
U.Enable = 'on';
R=java.awt.Robot;
R.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
R.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
elseif strcmp(U.Enable,'on')
U.Enable = 'inactive';
end
end
U.Enable

Community Treasure Hunt

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

Start Hunting!