Is it possible to assign keyboard shortcuts to trigger certain parts of my app in app designer, and if so, how?

조회 수: 139 (최근 30일)
I was wondering if it's possible to assign user defined keyboard shortcuts to parts of the app in app designer.
For example,using a menu item with ctrl+L, or simply just L. Not sure if this is possible, but if anyone knows, please share
Thanks!

채택된 답변

rbme17
rbme17 2020년 4월 7일
편집: rbme17 2020년 10월 27일
04/07/2020: Implement Basic Key Press Functionality
I found out what to do based on Mohammad Sami's reply to my question.
(1) Go to callbacks in the app designer window for the entire UIFigure in deisgn view
(2) In the 'keyboard' menu, go to KeyPressFcn and select <add KeyPressFcn callback> (should be at the top of the list). This will create a callback in code view for you to edit.
(3) In the callback, there will be a preset variable called 'key'. Set up a swtich/case for which key's you'd like to perform specific functions. 'key' will always providing the physical key pressed as a string in lowercase letters.
% Key press function: UIFigure
function UIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'p'
app.PlotMyData(app);
app.Message.Value = key; % return key value
case 'f1'
app.ChangeData(app);
app.Message.Value = key;
case 'shift'
app.ChangeColor(app);
app.Message.Value = key;
case '1'
app.Message.Value = 'Pressed 1';
end
end
(4) As a side note for those who may not know, you can get a look at what the value of 'key' will be by creating a text box, and setting the value to key (shown above). This will help you designate keys for functions you may have.
Hope this helps someone else as well.
Edit (10/27/2020): Use Modifiers (e.g. shift Q, control P, etc...) along with Key Press
The following code shows an example of how to use modifiers such as 'shift', 'control', and 'alt' with the keypress functionality.
You don't have to use a switch/case and can accomplish the same goal by using if/elseif/else of whatever you'd like. This is just one way I thought of doing this. This allows for things like ctrl+q, shift+p, etc.
Make sure to press them quickly in succession or it will only register one key press, either the modifier or the letter/number/etc. Hope this helps!
% Key press function: UIFigure
function UIFigureKeyPress(app, event)
key = event.Key;
if isequal(event.Modifier,'shift')
switch key
case 'f1'
app.ChangeData(app);
app.Message.Value = key;
case ...
...
end
elseif isequal(event.Modifier,'control')
switch key
case 'q'
app.Message.Value = 'quitting'; % write to text area of GUI
% insert a quit function %
case ...
...
end
else % no modifier used (e.g. just a key press, 'k','f4', etc.)
...
end
end
  댓글 수: 7
ol Poot
ol Poot 2023년 10월 23일
편집: ol Poot 2023년 10월 24일
the code above worked amazingly for me. Except, for me, the modified keys need " " not ' '
Tom Parish
Tom Parish 2023년 11월 2일
I am trying to use this same method to update Values from exiting buttons.
Is the app.ChangeData a button or a callback function?
In my usecase i have a State button app.STOPbutton and a callback app.StopButtonValueChanged. I am trying to update a global variable with a keyboard press that is already actiavted when the State button app.STOPButton is pressed through the app.StopButton Changed callback.
Any guidance is appreciated.
function figure1KeyPress(app, event)
key = event.Key;
switch key
case 's'
app.STOPButtonValueChanged(app);
app.Message.Value = key; % return key value
case 'o'
app.SafetySwitchValueChanged(app);
app.Message.Value = key; % return key value
case 'r'
app.RESUMEButton(app);
app.Message.Value = key;
end
end

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

추가 답변 (1개)

Abhisek Pradhan
Abhisek Pradhan 2020년 4월 2일
There is similar question in following link on configuring a GUI button to keypress. Hope it helps.https://in.mathworks.com/matlabcentral/answers/12034-gui-button-with-keyboard-shortcut
  댓글 수: 1
rbme17
rbme17 2020년 4월 6일
Hi Abhisek Pradhan,
Thanks for your response!
It seems like the solution provided implements 'uicontrol', which according to the MATLAB help database, is only suited for GUIDE. I am working with app designer, so I don't think it'll work for me.

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

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by