capture focus change of a figure

5 views (last 30 days)
Arabarra
Arabarra on 15 Feb 2017
Answered: DGM on 10 Nov 2024 at 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

Answers (1)

DGM
DGM on 10 Nov 2024 at 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.

Categories

Find more on Specifying Target for Graphics Output 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!