how to set controller by keyboard arrows in matlab app designer

16 visualizaciones (últimos 30 días)
Roye Vadana
Roye Vadana el 7 de Feb. de 2022
Editada: Gowtham el 27 de Sept. de 2023
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

Respuestas (1)

Gowtham
Gowtham el 12 de Sept. de 2023
Editada: Gowtham el 27 de Sept. de 2023
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

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by