App designer - how to make a simple button preselected so mouse cursor is not needed?

4 views (last 30 days)
Hi,
I've made an app in which an operator have to press a single button to obtain results each time (it's a cable harness tester).
It's a huge button on the screen so one can't miss it with a mouse. But it would be much more convenient if one could just press Space or Enter instead of having to use the mouse each time. However, the button loses it's pre-selection after pressing it.
Is there any way to control this? Maybe shortcuts?
Thanks in advance!

Accepted Answer

Cameron B
Cameron B on 23 Dec 2019
You'll start by going to the Design View tab and then looking at the Component Browser where all your components are listed. From there, you can right click on your figure name (by default it's app.UIFigure) and then go to Callbacks, then select Keyboard Callbacks, and choose Go to UIFigureKeyPress callback. This creates the callback function for when you hit the keyboard. Then paste the code below.
function UIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'e'
ButtonPushed(app,event); %or whatever your function is. This is the name of mine
end
end
If you want the code to work using the Enter key, then in place of 'e' you can put 'return' and this should work. The only thing you must remember is that you need to click on the figure itself for it to knokw that the key press is related to that specific GUI. For example, you cannot click on a separate window and click 'e' and expect your program to run. With this setup, you can also create more shortcuts. For example, if you wanted to do F1 then you would replace 'e' with 'f1'. Or you could add another case that would look like this:
function UIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'e'
ButtonPushed(app,event); %or whatever your function is. This is the name of mine
case 'f1'
msgbox('You pushed the F1 button')
case 'return'
msgbox('You pressed enter')
end
end
Remember to put in the required variables into your function! Hope this answers your question.
  1 Comment
yumba mumba
yumba mumba on 24 Dec 2019
Edited: yumba mumba on 24 Dec 2019
Thanks a lot! The answer is perfect. I couldn't find the correct and simple example of usage of this feature myself for some reason. Hope this helps others too.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!