GUI figure focus

29 views (last 30 days)
tomas
tomas on 23 Mar 2012
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
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.

Sign in to comment.

Answers (3)

Jan
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
undefined undefined on 28 Apr 2021
How can I know which figure is currently in focus ?
Jan , U gr8 !
TNX for all Ans' !

Sign in to comment.


Sean de Wolski
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
Jan on 23 Mar 2012
Some of the suggested methods do not work in Matlab 2008a. Which version are you using, tomas?
tomas
tomas on 26 Mar 2012
I use Matlab 7.11.0 (R2010b) version.
I made another testGUI because the original one is quite complicated for posting it here. I put there only necessary components to demonstrate where the problem is and the script behaves totaly different and I'm completely confused :-). I draw there the patch object in the same way and shortcuts works even while the patch object is still active. The only problem with the focus is after I delete the patch object. May be there is any connection why it is happen, but I'm not able to find out where.
How can I post the testGUI and data? Is there any possibility to do it within this site or just use any common upload site?
Thank you guys for your help.

Sign in to comment.


Stefan Karlsson
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.

Categories

Find more on Migrate GUIDE Apps 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!