how to set controller by keyboard arrows in matlab app designer

14 ビュー (過去 30 日間)
Roye Vadana
Roye Vadana 2022 年 2 月 7 日
編集済み: Gowtham 2023 年 9 月 27 日
I have a problem to set controller by keyboard arrows in matlab app designer. I cant reset my key value when there is no key Pressed. I want to make action only while some key is pressing and stop the action when the key is un pressing
function ControllerUIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'leftarrow'
app.left_icon.Position = [210 98 71 102];
case 'uparrow'
app.up_icon.Position = [285 150 71 102];
case 'rightarrow'
app.right_icon.Position = [360 98 71 102];
case 'downarrow'
app.down_icon.Position = [285 40 71 102];
otherwise
app.down_icon.Position = [285 60 71 102];
app.up_icon.Position = [285 133 71 102];
app.right_icon.Position = [343 98 71 102];
app.left_icon.Position = [225 98 71 102];
end

回答 (1 件)

Gowtham
Gowtham 2023 年 9 月 12 日
編集済み: Gowtham 2023 年 9 月 27 日
Hello Roye Vadana,
I understand that you want to change the position of arrow icons using keyboard arrows in app designer. To reset the position of the keys, we can use the Key Release callback which is similar to the Key Press callback you have created.
For a sample demonstration of this process, kindly refer to the updated code snippet:
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.down_icon.Position = [285 60 71 102];
app.up_icon.Position = [285 133 71 102];
app.right_icon.Position = [343 98 71 102];
app.left_icon.Position = [225 98 71 102];
end
% Key press function: UIFigure
function UIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'leftarrow'
app.left_icon.Position = [210 98 71 102];
case 'uparrow'
app.up_icon.Position = [285 150 71 102];
case 'rightarrow'
app.right_icon.Position = [360 98 71 102];
case 'downarrow'
app.down_icon.Position = [285 40 71 102];
otherwise
app.down_icon.Position = [285 60 71 102];
app.up_icon.Position = [285 133 71 102];
app.right_icon.Position = [343 98 71 102];
app.left_icon.Position = [225 98 71 102];
end
end
% Key release function: UIFigure
function UIFigureKeyRelease(app, event)
key = event.Key;
% reset positions
switch key
case 'leftarrow'
app.left_icon.Position = [225 98 71 102];
case 'uparrow'
app.up_icon.Position = [285 133 71 102];
case 'rightarrow'
app.right_icon.Position = [343 98 71 102];
case 'downarrow'
app.down_icon.Position = [285 60 71 102];
end
end
end
Kindly refer to the following documentation for further understanding on callbacks in app designer https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html
Hope this helps in resolving the issue you were facing.
Regards,
Gowtham

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by