Window key press function and Slider confliction

10 visualizzazioni (ultimi 30 giorni)
Hello, I need to use arrow key to move the crosshair in the figure. This function (crosshair moving) works well in GUIDE and app designer. However, the window key press function behaves differently between app designer and GUIDE.
Let's say I have some buttons, a slider, and an axes/uiaxes in the window. The slider is used to control the gamma of the image.
In GUIDE, when the slider is focused and pressing arrow key, it won't trigger the WindowKeyPressFcn, which means the slider moves (so the gamma changes), but the crosshair in the figure (axes1) doesn't move.
However, in app designer, when the slider is focused, pressing arrow key will trigger both the slider and Window key press function. This results in both gamma changing and crosshair moveing. I don't want such behavior.
Is there a way to make this function behave the same as in the GUIDE?
  5 Commenti
Tianlun Yu
Tianlun Yu il 6 Ott 2025 alle 12:09
Well I got the reply from Matlab team. Your idea is correct, but the code is not entirely correct. The current focused component should be called by "event.Source.CurrentObject" instead of "event.Source". One more step down...
dpb
dpb il 6 Ott 2025 alle 13:52
OK, thanks. I didn't see any reference in the documentation that there were additional fields; think that would be worth submitting as documentation enhancement. Of course if one is in the debugger one can go poking and would be able to see it, but would have to think of that being what had been done with no prior hints.

Accedi per commentare.

Risposta accettata

Oliver Jaehrig
Oliver Jaehrig il 6 Ott 2025 alle 13:48
Here you can find some information of my response in the TS case which should clarify this issue:
As far as I see the callback works as documented:
"This callback function executes when the user presses a key while the figure or a child component has focus. If the user presses multiple keys at approximately the same time, MATLAB detects all the keys."
So when there is not a KeyPressFcn on the component level (here the uislider object),
I recommend to do something as follows:
Do an early return in the WindowKeyPressFcn callback by checking if the CurrentObject property of the uifigure shows that it is a slider object and then return.
This will solve that the code gets executed which should not run when a slider is focused.
Here you can find an example:
function UIFigureWindowKeyPress(app, event)
currentObj = event.Source.CurrentObject;
if class(currentObj) == "matlab.ui.control.Slider"
return
end
%will not run when a Slider is current object
key = event.Key;
disp(key)
end
  3 Commenti
Oliver Jaehrig
Oliver Jaehrig il 6 Ott 2025 alle 14:55
@dpb Thank you for your feedback. The comment you cited was more about me writing when the callback actually executes and not when talking about our documentation and the Source property.
Sorry for the confusion.
I agree that the documentation of event.Source can be improved and I forwarded your feedback to our development team to review this documentation to check if it should be improved in a future release.
If you have any further feedback, please let me know.
dpb
dpb il 6 Ott 2025 alle 15:37
Modificato: dpb il 7 Ott 2025 alle 3:40
" the documentation of event.Source can be improved and I forwarded your feedback to our development team to review this documentation to check if it should be improved in a future release."
My additional comment would be the use of "if" in the above... <g>
IMO, doc isn't complete until there is a link to all objects referenced showing all detail as well as use examples more on the order of @Tianlun Yu's situation in this case. This isn't trivial and does seem convoluted in comparison to GUIDE behavior presuming (as I have little/no doubt) the actual triggering of the callbacks is as described.
As far as the point whether the comment was focused on when the callback executes, the code only works if one knows a priori how to get the actual component reference and that's where my prior response fell short because I had no way to know had to look at a field lower than the top level since it would seem the "source" of the callback would have been the slider.
I don't build complex GUIs; my MATLAB use is exclusively for processing of data I collect for my own use or in support of clients for which I prepare final reports rather than providing tools for the same so I don't have direct experience in such depth despite 30+ years w/ MATLAB. I also have not delved into R2025 as there's little there that matters for my purposes -- while perhaps a disadvantage in Answers in that don't have the environment locally to experiment with, it gives me the advantage that if I can't figure it out from the documentation alone, then other users aren't likely to be able to, either.
ADDENDUM
I guess I do have another comment after all... <g>
The extra logic and finding out which control is the culprit is needed in the window keypress callback because "... there is not a KeyPressFcn on the component level ..."
My comment here then is why is the uislider matlab.ui.control.Slider class hamstrung by not having an event callback thus forcing such code branching logic? This would get very convoluted very quickly and extremely difficult to code/debug it would seem if there are a number of controls in an app with these kinds of interactions.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti


Release

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by