capture focus change of a figure

5 次查看(过去 30 天)
Arabarra
Arabarra 2017-2-15
回答: DGM 2024-11-10,14:05
Hi,
does somebody know how to listen to a focus change of a figure? Yair Altman's blog mentions a way in his blog: http://undocumentedmatlab.com/blog/detecting-window-focus-events but unfortunately this method doesn't work if the figure includes a graphical axis.
Any hint appreciated! Daniel

回答(1 个)

DGM
DGM 2024-11-10,14:05
This is based on the examples given in the comments. I've tested it in R2015b and R2019b, so I'm assuming it would have worked circa 2017. This does not work on docked figures.
% you have a populated figure (has a descendant axes)
hFig = figure;
plot(rand(100,1));
drawnow
% mute the warning
% i don't know which exact version this changed in
warning off 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame' % older versions (effective in R2015b)
warning off 'MATLAB:ui:javaframe:PropertyToBeRemoved' % newer versions (effective in R2019b)
% Get the underlying Java reference
jf = get(hFig,'JavaFrame');
jw = handle(jf.getFigurePanelContainer.getTopLevelAncestor,'CallbackProperties');
% set some example focus callbacks
set(jw,'WindowGainedFocusCallback',@(x,y)disp('given focus'));
set(jw,'WindowLostFocusCallback', @(x,y)disp('lost focus'));
That said, I don't know if this still works in modern versions. It fails in the forum editor, though I'm not sure if that's just a forum thing or if the JavaFrame property has finally been removed. Unfortunately, that seems particularly likely.

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by