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.
function UIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'p'
app.PlotMyData(app);
app.Message.Value = key;
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!
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';
case ...
...
end
else
...
end
end
Best Answer