anyone?
How to disable uicontrol 'mouseless' interaction
4 views (last 30 days)
Show older comments
Dear community,
at the moment I'm creating a gui using normal figures and uicontrol elements. One side is a plotting window in which the user can interact using key press and mouse klick, the other half of the figure are some additional settings like visibility of certain elements etc using checkboxes for example
If I click on a checkbox and afterwards i press 'space' in the plotting window which is processed by ''WindowButtonDownFcn''.
My Problem is, that at the same time the 'space' changes the checkbox value
Similar things happen if I changed the value of a popupmenu and I want to navigate in the plotting window using the arrow keys.
The Callbacks of the 'WindowButtonDownFcn' are processed correctly, but at the same time i change the value of the uicontrol
How can I remove this behavior such that the user can not interact with uicontrol elements using keyboard button press (and also navigating using TAB once an uicontrol element was used) but only mouse clicks
I am using Win10 and Matlab 2021a
here a short "working" example
close all;
fig=figure;
% create plots and disable interactivity
s=subplot(1,2,1);
p=plot(rand(1,1000));
axtoolbar(s,'Visible','off');
disableDefaultInteractivity(s);
%% create buttons and set window key press function
pressedButton= uicontrol(fig,'Style','text');
pressedButton.Units='normalized';
pressedButton.Position = [0.7 0.7 0.2 0.04];
pressedButton.String = 'no key pressed yet';
fig.WindowKeyPressFcn=@(~,ev) set(pressedButton,'String',ev.Key);
colorPopup = uicontrol(fig,'Style','popupmenu');
colorPopup.Units='normalized';
colorPopup.Position = [0.7 0.5 0.2 0.04];
colorPopup.String = {'r','b','k','c'};
colorPopup.Value = 1;
colorPopup.Callback =@(~,~) set(p,'Color',colorPopup.String{colorPopup.Value});
visibilityCheckbox = uicontrol(fig,'Style','checkbox');
visibilityCheckbox.Units='normalized';
visibilityCheckbox.Position = [0.7 0.6 0.2 0.04];
visibilityCheckbox.String='Visibility';
visibilityCheckbox.Value=1;
visibilityCheckbox.Callback=@(~,~) set(p,'Visible',visibilityCheckbox.Value);
best regards
Jonas
Accepted Answer
Jan
on 25 Apr 2022
If you click on an uicontrol element, it gets the focus. Then following keyboard events are caught be the keyboard handling of the uicontrol. A solution is to give the focus back to the figure or the axes in the callback of the uicontrol. This should work by figure(fighandle) according to the documentation, but this was not successful at least for Matlab 6.5 to 2018b (the newest version I have).
Workarounds:
- Insert code in the callbacks, which sets the "Enable" property of the uicontrol to 'off' temporarily:
function checkBoxCallback(ObjH, EventData)
set(ObjH, 'Enable', 'off');
drawnow;
set(ObjH, 'Enable', 'on');
pause(0.02); % Voodoo!
end
- Under Windows, this can help: https://www.mathworks.com/matlabcentral/fileexchange/31437-windowapi
WindowAPI(figHandle, 'setFocus')
10 Comments
More Answers (1)
See Also
Categories
Find more on Interactive Control and Callbacks 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!