- “CurrentObject”: https://www.mathworks.com/help/releases/R2023a/matlab/ref/matlab.ui.figureappd-properties.html?searchHighlight=currentObject
- “focus”: https://www.mathworks.com/help/releases/R2023a/matlab/ref/matlab.ui.figure.focus.html
- “event.Key” and “event.Modifier”: https://www.mathworks.com/help/releases/R2023a/matlab/ref/matlab.ui.figure-properties.html?searchHighlight=KeyPressFcn
Ignore uicomponents when pressing the tab key in App Designer
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have an GUI in AppDesigner and I'm using the tab key to jump from one component to other but I need to find a way to ignore some uicomponents without setting the 'Enable' Property of the component to ignore to 'off'.
As an example, in the attached gif you can see what is happening right now and what I expect to do. I have two Edit Fields and a uitable and what I want to do is to go from the Edit Field1 to Edit Field2 pressing the Tab Key ignoring the uitable.
I know I can get a similar behavior chaning the order of the components in the Component Browser but this has a problem which is that If I jump from Edit Field1 to Edit Field2 and press the Tab Key again it will take me to the uitable. In this case, the expected behavior will be that once I lay on the Edit Field2 and press Tab Ket again, it will take me to the Edit Field1.
Finally, I can't set the 'Enable' property of the ignored component to 'off' because I use that component. (In the example suppose that the uitable has a Celledit callback that I will use in other moment)
PD: The example is purely ilustrative, please don't put your attention in the design or other aspects.
Thanks in advance,
0 commentaires
Réponses (1)
Sreeram
le 18 Sep 2024
Hi Juan,
It is possible to shift focus only between certain components in MATLAB App Designer by coding the actions that the “Tab” key should perform in the window key press callback of the “UIFigure”.
Here are the steps to do it:
1. Create two private properties ‘editFields’ and ‘curEditFld’ to the class of the app. The ‘editFields’ property will store the desired UI Components we want to limit the focus to. The ‘curEditFld’ property will be used to index into the desired components.
properties (Access = private)
editFields;
curEditFld;
end
2. Initialize them in the startup function. Since it is required that the focus be limited to “EditField” components, we store them as a list in the ‘editFields’ property. For this example, there are 4 “EditField” components.
function startupFcn(app)
app.editFields = [app.EditField1, app.EditField2, app.EditField3, app.EditField4];
app.curEditFld = -1;
end
3. Now in the “UIFigureWindowKeyPress“ callback of “app.UIFigure”, specify how to handle “Tab” key press. Perform modular addition by '1' on the ‘curEditFld’ property and use it to index into the list of “EditField” components to get the component to be focused. In a similar way, handle cases like pressing “Shift+Tab” and using the mouse to randomly set the focus on specific components.
function UIFigureWindowKeyPress(app, event)
if strcmp(event.Key, "tab")
if size(event.Source.CurrentObject) ~= [0 0]
% Handle cases where user manually changes the current
% object through mouse click
actualIndex = find(app.editFields == event.Source.CurrentObject);
if(actualIndex-1~=app.curEditFld)
app.curEditFld=actualIndex-1;
end
end
if isempty(event.Modifier) % Tab : Move forward
app.curEditFld = mod(app.curEditFld+1,length(app.editFields));
elseif strcmp(event.Modifier{:},'shift') % Shift+Tab : Move backward
app.curEditFld = mod(app.curEditFld-1,length(app.editFields));
end
% Set the focus and current object to desired component
focus(app.editFields(1+app.curEditFld))
event.Source.CurrentObject = app.editFields(1+app.curEditFld);
end
end
This will “override” the default behavior of pressing “Tab”.
Please refer to the documentation links below to understand more on the properties and functions used:
I hope it helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!