How use keyboard arrow keys to control the slider GUI in App Designer?

조회 수: 22 (최근 30일)
tonybabel
tonybabel 2019년 2월 14일
댓글: Vitek Stepien 2022년 8월 23일
GUIslider.png
Hi,
I have trouble using keyboard input (like arrow keys) to control a slider GUI in App Designer. For example, when pressing the left arrow key, the slider cursor move to left and pressing the right arrow the slider cursor move to right. I'm trying to use switch/case but totally have no idea what need to be written under each 'case'. Please help! Thank you so much!
Below is the codes I grabbed from "code view" window in App Designer.
% Value changing function: Slider
function SliderValueChanging(app, event)
changingValue = event.Value;
set_param('Output','Value',num2str(changingValue));
end
% Key press function: UIFigure
function UIFigureKeyPress(app, event)
switch event.Key
case 'left arrow key'
??????
case 'right arrow key'
??????
end

답변 (2개)

Roche de Guzman
Roche de Guzman 2021년 1월 8일
Under the keypress fx callback:
function UIFigureKeyPress(app, event)
value = app.Slider.Value; % get the slider value
key = event.Key; % get the pressed key value
if strcmp(key,'leftarrow')
value = value-1; % left value
elseif strcmp(key,'rightarrow')
value = value+1; % right value
end
app.Slider.Value = value; % set the slider value
SliderValueChanging(app, event); % execute the slider callback
end
  댓글 수: 1
Vitek Stepien
Vitek Stepien 2022년 8월 23일
This helped me, very simple way. The only caveat is that when I change tabs I have to click somewhere else, or the left-right keys change tabs, and when I use the Zoom tool on an axis I have to deactivate it because the arrows pan the plot. But I know this can be overriden if someone really cares.

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


Rik
Rik 2019년 2월 14일
You can use this tester GUI to determine what you should put for the special keys. Don't forget the otherwise option to exit the function if you don't want to respond to other keys.
figure(1),clf(1)
set(gcf,'KeyPressFcn',@keycall)
function keycall(h,e)
disp(e.Key)
end
%rightarrow and leftarrow are the left and right arrow key names
  댓글 수: 2
Rik
Rik 2019년 2월 18일
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
Markus Leuthold
Markus Leuthold 2022년 7월 22일
3 years later, this is still a problem. @Rik your solution considers the old java based control, OP talked about the new web based controls
  • uicontrol('style', 'slider',....) lets you control the slider with the keyboard and has a KeyPressFcn callback
  • uislider doesn't let you control the slider with the keyboard and has no keyboard related callback
This is a serious shortcoming

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by