App designer Key-press callback

102 views (last 30 days)
Paolo Neri
Paolo Neri on 10 Mar 2021
Edited: Adam Danz on 3 Nov 2023
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 Comments
Kelsey Bettridge
Kelsey Bettridge on 13 Dec 2021
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!
Paolo Neri
Paolo Neri on 14 Dec 2021
No, I could not solve the issue and I did not get any suggestion.. if you do, please post!

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 15 Dec 2021
Edited: Adam Danz on 16 Dec 2021
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.
  9 Comments
Adam Danz
Adam Danz on 6 Feb 2023
Edited: Adam Danz on 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 on 3 Nov 2023
Edited: Jixiong Su on 3 Nov 2023
Thank you for your answer!
However, In 2023b, It would be better to use WindowsKeyPress rather than KeyPress.

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!