Keyboard Shortcut to move through the tabs in a Tab Group menu (Request to improve code)

15 views (last 30 days)
Howdy!
I've been messing around with trying to have a way to move through the tabs in a Tab Group object via the keyboard. I've got a solution, but I feel like there's probably a more elegant route. I'm looking to see if there are any thoughts on improving what i've got here.
One thing i couldn't figure out is how to get a tab handler so i don't have to constantly refer to app.TabGroup....
If nothing else, then I thought maybe posting what i've got for a solution could be helpful to others.
function UIFigureKeyPress(app, event)
key = event.Key; %capture the regular key that was pressed
eventKey = event.Modifier; %capture any modifier key that was pressed
if isequal(eventKey, "control")
switch key
case 'pagedown'
totalTabs = numel(app.TabGroup.Children); %need the total number of tabs in the TabGroup. May inc/dec at some point
app.TabGroup.SelectedTab.ForegroundColor = "Black"; %return the active tab text color to black
if app.TabGroup.SelectedTab == app.TabGroup.Children(1) %if the active tab is the first tab then the next tab 'down' is looping back to the last tab
app.TabGroup.SelectedTab = app.TabGroup.Children(totalTabs);
else
for i = 2:totalTabs %otherwise the next tab down is just acitve tab - 1
if app.TabGroup.SelectedTab == app.TabGroup.Children(i)
app.TabGroup.SelectedTab = app.TabGroup.Children(i-1);
break;
end
end
end
app.TabGroup.SelectedTab.ForegroundColor = "Blue"; %set the active tab text color to blue to indicate active tab
case 'pageup'
totalTabs = numel(app.TabGroup.Children);
app.TabGroup.SelectedTab.ForegroundColor = "Black";%return the active tab text color to black
if app.TabGroup.SelectedTab == app.TabGroup.Children(7) %if the active tab is the last tab, the the next tab 'up' is looping to the first tab
app.TabGroup.SelectedTab = app.TabGroup.Children(1);
else
for i = 1:totalTabs-1 %otherwise the next tab up is active tab + 1
if app.TabGroup.SelectedTab == app.TabGroup.Children(i)
app.TabGroup.SelectedTab = app.TabGroup.Children(i+1);
break;
end
end
end
%set the active tab text color to blue to indicate active tab
end
end
end
end

Accepted Answer

Voss
Voss on 25 Oct 2023
Edited: Voss on 25 Oct 2023
That code can be simplified a bit:
% Window key press function: UIFigure
function kpf(app, event)
if ~isequal(event.Modifier, "control")
return
end
if strcmp(event.Key,'pagedown')
direction = -1;
elseif strcmp(event.Key,'pageup')
direction = +1;
else
return
end
tg = app.TabGroup;
tg.SelectedTab.ForegroundColor = "Black"; %return the active tab text color to black
idx = find(tg.Children == tg.SelectedTab); % index of the active tab
new_idx = mod(idx+direction-1,numel(tg.Children))+1; % index of the new tab
tg.SelectedTab = tg.Children(new_idx); % make the new tab active
tg.SelectedTab.ForegroundColor = "Blue"; %set the active tab text color to blue to indicate active tab
end
See the attached app.
Other changes made include:
  • Defining a SelectionChangedFcn for TabGroup that updates the tab buttons' ForegroundColor.
  • Calling that SelectionChangedFcn from a StartUpFcn in order to initialize the ForegroundColors when the app starts.
  • Using WindowKeyPressFcn instead of KeyPressFcn because KeyPressFcn didn't execute when a tab button itself had focus (i.e., click a tab, hit ctrl+pageup, nothing happened; click outside a tab, hit ctrl+pageup, it works).
  7 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!