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

조회 수: 9 (최근 30일)
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

채택된 답변

Voss
Voss 2023년 10월 25일
편집: Voss 2023년 10월 25일
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
ol Poot
ol Poot 2023년 10월 26일
이동: Voss 2023년 10월 26일
ah doh! sorry. missed that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by