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

2 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Cameron B
Cameron B el 23 de Dic. de 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 comentario
yumba mumba
yumba mumba el 24 de Dic. de 2019
Editada: yumba mumba el 24 de Dic. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by