GUI figure focus
34 views (last 30 days)
Show older comments
Hello, I have a problem with focusing of the main figure in my GUI. My keyboard shortcuts works properly until I use the fill command for adding a patch object to my plot. While this object exists I can't use keyboard shortcuts, but immediately after deleting the patch object I'm able to use all shortcuts again. I've read discusion about similar problems of other users and I've tried set(hObject, 'enable', 'off'); drawnow; set(hObject, 'enable', 'on');
but it doesn't work for this.
function ReleaseFocus(fig) mentioned in one answer has no effect as well.
Any ideas how to solve this problem?
Thanks in advance.
Tom
1 Comment
Oleg Komarov
on 23 Mar 2012
Please post a synthetic version of your gui and which keyboard shortcuts you're trying to gian focus on the figure.
Answers (3)
Jan
on 23 Mar 2012
Actually this should move the focus to the figure:
figure(gcf)
Unfortunately this does not work - at least since Matlab 5.3.
The temporary disabling of an uicontrol, which is currently focussed, move the focus to its parent. If your GUI contains an uicontrol you can try this, after the drawing:
hObject = HandleOfTheButton; % Set accordingly
uicontrol(hObject); % Yes, this works correctly!
set(hObject, 'enable', 'off');
drawnow;
set(hObject, 'enable', 'on');
This moves the focus to the button (or whatever) at first and to the figure afterwards.
But a problem remains: Whenever you click on the axes object, it gets the focus back and the figure's KeyPressFcn is not active anymore. Then it will be more reliable, if you add the KeyPressFcn to all objects, which allows to set this property.
The must be a Java callback, which allows to activate the figure, but I cannot test this currently:
jPeer = get(handle(gcf), 'JavaFrame');
jAxis = jPeer.getAxisComponent;
jWindow = jPeer.fFigureClient.getWindow;
Now you can study the methods:
methods(jPeer);
methods(jAxis);
methods(jWindow);
Is there a "setFocus()" routine? If so and it works:
This is undocumented and you have to care for the fact, that this might not work with other Matlab releases. So include the command in a TRY-CATCH block and write a meaningful warning message if it fails.
[EDITED] It is:
jPeer = get(handle(gcf), 'JavaFrame')
jPeer.getAxisComponent.requestFocus
[EDITED 2] Please send an enhancement request to TMW, if you want figure(gcf) to set the focus reliably.
1 Comment
undefined undefined
on 28 Apr 2021
How can I know which figure is currently in focus ?
Jan , U gr8 !
TNX for all Ans' !
Sean de Wolski
on 23 Mar 2012
What is hObject in the above line? I am going to hazard guess that it is not the axes. Can you put a break point right before you do the:
set(hOb...
And then:
get(hObject,'type')
6 Comments
Jan
on 23 Mar 2012
Some of the suggested methods do not work in Matlab 2008a. Which version are you using, tomas?
Stefan Karlsson
on 5 Feb 2015
Seems this is still an issue, here is my solution quick fix:
%takes any active uiobject as input, and returns keyboard focus back to parent window
function ReleaseFocusFromAnyUI(uiObj)
set(uiObj, 'Enable', 'off');
drawnow update;
set(uiObj, 'Enable', 'on');
this works well on a couple of systems I tried it on. The addition of "update" to the drawnow command makes it execute faster on my systems, and prevents the drawnow to issue any new events that makes callbacks execute (a hidden issue with above suggestion).
I put this at the very front of my callbacks.
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!