GUI figure focus
31 次查看(过去 30 天)
显示 更早的评论
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 个评论
Oleg Komarov
2012-3-23
Please post a synthetic version of your gui and which keyboard shortcuts you're trying to gian focus on the figure.
回答(3 个)
Jan
2012-3-23
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 个评论
undefined undefined
2021-4-28
How can I know which figure is currently in focus ?
Jan , U gr8 !
TNX for all Ans' !
Sean de Wolski
2012-3-23
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 个评论
Jan
2012-3-23
Some of the suggested methods do not work in Matlab 2008a. Which version are you using, tomas?
Stefan Karlsson
2015-2-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!