GUI figure focus
조회 수: 29 (최근 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!