App designer Key-press callback

I have a uifigure with some buttons and an axes, which show a pointcloud in realtime. I added to the uifigure a Key-press callback, so that when the keyboard is pressed, some actions are performed. This works generally fine. My problem is that I need to click a pushbutton to start the pointcloud streaming. After pushing the button, the Key-press callback does not work anymore, maybe because the focus is on the button, and not on the figure. Then, if I click with the mouse on the axis, then the Key-press callback works again: is there a way to avoid or automatize this mouse click? I found the solution exploiting java (import java.awt.Robot; import java.awt.event.*; mouse = Robot;), i.e. programmatically move the mouse pointer to the axis and click it, but it is not the proper way to solve my issue and I would like something more robust. Any suggesiont?

4 Commenti

Mario Malic
Mario Malic il 10 Mar 2021
Modificato: Mario Malic il 10 Mar 2021
Instead of KeyPressFcn callback on the Axes, set it on the uifigure, so the key is registered wherever you click in the uifigure. You also might need to filter out the Sources, for example, if your Source is a button, then it will trigger a callback (not completely sure), when you pressed it.
Indeed, the callback is applied to the uifigure, and not to the axis. I've addedd the callback to "keyboard" section. I alsow tryed to add the callback to the window but the behavior did not change.
Did you ever figure this one out? I'm having the same problem. I think you are correct about the focus, but I'm having trouble coding a workaround to get the focus back on the figure. Maddening!
No, I could not solve the issue and I did not get any suggestion.. if you do, please post!

Accedi per commentare.

Risposte (1)

Adam Danz
Adam Danz il 15 Dic 2021
Modificato: Adam Danz il 16 Dic 2021

2 voti

Attached is a simple demo app that contains a KeyPressFcn assigned to the uifigure and a button. The KeyPressFcn merely prints the key pressed to the command window.
At the end of the ButtonPushed callback function, the figure is activated using figure(app.UIFigure) so that it continues to respond to key strokes. This works in Matlab R2021a and later.

10 Commenti

Thank you fro your demo, I tried it but unfortunately when I push the button, but unfortunately when I push the button the keyboard is not detected anymore. Then, if I click on the figure, it works again. I'm using Matlab R2020a
Have you tested my demo? I tested it in R2021a and it works as described. That suggests there may be a problem with your implementation of the solution within your app or perhaps the callback function is calling an external file that is interfering. The call to figure() should be at the end of the callback function, assuming execution reaches the end of the function.
Alternatively, you could add
figCleanup = onCleanup(@()figure(app.UIFigure));
at the top of the button callback function and the figure will become current as soon as execution exits the callback function.
Sorry, I just realized you wrote "R2020a". I don't have that release on my machine but I have R2020b and now I see the behavior you're describing. Let me rethink it....
Hi Adam! Thanks so much. I had tried this but was using R2019b. When I switched to R2021a, it worked. Hopefully this helps others who encounter this issue- don't be lazy like me and not update to the latest version :)
Thanks for the feedback @Kelsey Bettridge.
Sorry @Paolo Neri, I haven't succeeded in coming up with a workaround for older releases. I tried using a java robot to activate the figure but couldn't get it to work; I tried using UI Test gestures to simulate a mouse click on the figure space but that testing framework is not supported with grapics obj callbacks. And I tried a handfull of other solution to make the uifigure the current object in r2020b but without success.
Hi Adam,
thank you for your effort, if it works on 2021 I'll consider updating my release!
Best regards
Paolo
Thank you for your answer!
Actually, it works in Matlab 2021 but it doesn't work in Matlab 2022a! I'm a little confused because usually newer versions contain the previous features!
Do you have any solution for 2022?
Adam Danz
Adam Danz il 6 Feb 2023
Modificato: Adam Danz il 3 Nov 2023
I just tested it in R2022a and didn't come across any issues. The app has two new features.
  1. When the app is in focus, when you type on the keyboard the characters will appear in the command window.
  2. When you press the button, it merely puts the app figure in focus. Since the app figure is already in focus, you won't see anything happen when pressing the button.
If you are having trouble with the first item, make sure the app figure is in focus - it should have a blue title bar.
Jixiong Su
Jixiong Su il 3 Nov 2023
Modificato: Jixiong Su il 3 Nov 2023
Thank you for your answer!
However, In 2023b, It would be better to use WindowsKeyPress rather than KeyPress.
Zel Hurewitz
Zel Hurewitz il 17 Lug 2024
Modificato: Zel Hurewitz il 17 Lug 2024
In 2024a, zooming in a UIAxis seems to break the UIFigureWindowsKeyPress callback, maybe also for a similar reason about the figure being in focus.
I submitted this as its own question as well here.

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange

Prodotti

Release

R2020a

Richiesto:

il 10 Mar 2021

Modificato:

il 17 Lug 2024

Community Treasure Hunt

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

Start Hunting!

Translated by