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

2 次查看(过去 30 天)
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!

采纳的回答

Cameron B
Cameron B 2019-12-23
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 个评论
yumba mumba
yumba mumba 2019-12-24
编辑:yumba mumba 2019-12-24
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by