<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Detecting window focus events	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/detecting-window-focus-events/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=detecting-window-focus-events</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 14 Jun 2018 05:31:22 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Alexander K.		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-429772</link>

		<dc:creator><![CDATA[Alexander K.]]></dc:creator>
		<pubDate>Thu, 14 Jun 2018 05:31:22 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-429772</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824&quot;&gt;Fabio&lt;/a&gt;.

&lt;pre lang=&quot;matlab&quot;&gt;
% R2018a
hFig = figure; 
hAxes = axes(hFig);
plot(rand(100,1),&#039;Parent&#039;,hAxes);

% JavaSceneServerGLJPanel=get(hAxes.NodeParent.JavaComponent,&#039;Component&#039;)
% hJavaSceneServerGLJPanel=handle(JavaSceneServerGLJPanel,&#039;CallbackProperties&#039;);
%  
% set(hJavaSceneServerGLJPanel,&#039;FocusGainedCallback&#039;,@(x,y)disp(&#039;in focus&#039;));
% set(hJavaSceneServerGLJPanel,&#039;FocusLostCallback&#039;,  @(x,y)disp(&#039;... out&#039;));

drawnow

jf=get(hFig,&#039;JavaFrame&#039;);
jw=handle(jf.getFigurePanelContainer.getTopLevelAncestor,&#039;CallbackProperties&#039;);
 
set(jw,&#039;WindowGainedFocusCallback&#039;,@(x,y)disp(&#039;in focus&#039;));
set(jw,&#039;WindowLostFocusCallback&#039;,  @(x,y)disp(&#039;... out&#039;));

   &lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824">Fabio</a>.</p>
<pre lang="matlab">
% R2018a
hFig = figure; 
hAxes = axes(hFig);
plot(rand(100,1),'Parent',hAxes);

% JavaSceneServerGLJPanel=get(hAxes.NodeParent.JavaComponent,'Component')
% hJavaSceneServerGLJPanel=handle(JavaSceneServerGLJPanel,'CallbackProperties');
%  
% set(hJavaSceneServerGLJPanel,'FocusGainedCallback',@(x,y)disp('in focus'));
% set(hJavaSceneServerGLJPanel,'FocusLostCallback',  @(x,y)disp('... out'));

drawnow

jf=get(hFig,'JavaFrame');
jw=handle(jf.getFigurePanelContainer.getTopLevelAncestor,'CallbackProperties');
 
set(jw,'WindowGainedFocusCallback',@(x,y)disp('in focus'));
set(jw,'WindowLostFocusCallback',  @(x,y)disp('... out'));

   </pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Alexander K.		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-429736</link>

		<dc:creator><![CDATA[Alexander K.]]></dc:creator>
		<pubDate>Thu, 14 Jun 2018 00:52:09 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-429736</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824&quot;&gt;Fabio&lt;/a&gt;.

&lt;pre lang=&quot;matlab&quot;&gt;
% R2018a
hFig = figure; 
hAxes = axes(hFig);
plot(rand(100,1),&#039;Parent&#039;,hAxes);

JavaSceneServerGLJPanel=get(hAxes.NodeParent.JavaComponent,&#039;Component&#039;)
hJavaSceneServerGLJPanel=handle(JavaSceneServerGLJPanel,&#039;CallbackProperties&#039;);
 
set(hJavaSceneServerGLJPanel,&#039;FocusGainedCallback&#039;,@(x,y)disp(&#039;in focus&#039;));
set(hJavaSceneServerGLJPanel,&#039;FocusLostCallback&#039;,  @(x,y)disp(&#039;... out&#039;));
   &lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824">Fabio</a>.</p>
<pre lang="matlab">
% R2018a
hFig = figure; 
hAxes = axes(hFig);
plot(rand(100,1),'Parent',hAxes);

JavaSceneServerGLJPanel=get(hAxes.NodeParent.JavaComponent,'Component')
hJavaSceneServerGLJPanel=handle(JavaSceneServerGLJPanel,'CallbackProperties');
 
set(hJavaSceneServerGLJPanel,'FocusGainedCallback',@(x,y)disp('in focus'));
set(hJavaSceneServerGLJPanel,'FocusLostCallback',  @(x,y)disp('... out'));
   </pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Daniel Castano		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-400400</link>

		<dc:creator><![CDATA[Daniel Castano]]></dc:creator>
		<pubDate>Thu, 16 Feb 2017 09:41:18 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-400400</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824&quot;&gt;Fabio&lt;/a&gt;.

Well, I found a solution (R2016b) by trial and error for very simple situations, i.e., just one axes inside the figure. 
I just check the number of components in jAxis and grab the last one. 
&lt;pre lang=&quot;matlab&quot;&gt;
hFig = figure; 
h = axes(hFig);
plot(rand(100,1),&#039;Parent&#039;,h);
jFig = get(hFig, &#039;JavaFrame&#039;);
jAxis = jFig.getAxisComponent;

n = jAxis.getComponentCount;
disp(n);
jAxis.grabFocus();
drawnow; % will not work without this
n = jAxis.getComponentCount;
disp(n);

set(jAxis.getComponent(n-1),&#039;FocusGainedCallback&#039;,@(x,y)disp(&#039;in focus&#039;));
set(jAxis.getComponent(n-1),&#039;FocusLostCallback&#039;,  @(x,y)disp(&#039;... out&#039;));
&lt;/pre&gt;
Note that this is mainly cargo cult: I have no idea why I need to grab the focus and force a redraw for this to work. Still would be thankful if somebody comes up with a better, explainable solution, because the one I suggest here does not work for more complicated situations, such as GUIs with multiple panels and changing number of elements...]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824">Fabio</a>.</p>
<p>Well, I found a solution (R2016b) by trial and error for very simple situations, i.e., just one axes inside the figure.<br />
I just check the number of components in jAxis and grab the last one. </p>
<pre lang="matlab">
hFig = figure; 
h = axes(hFig);
plot(rand(100,1),'Parent',h);
jFig = get(hFig, 'JavaFrame');
jAxis = jFig.getAxisComponent;

n = jAxis.getComponentCount;
disp(n);
jAxis.grabFocus();
drawnow; % will not work without this
n = jAxis.getComponentCount;
disp(n);

set(jAxis.getComponent(n-1),'FocusGainedCallback',@(x,y)disp('in focus'));
set(jAxis.getComponent(n-1),'FocusLostCallback',  @(x,y)disp('... out'));
</pre>
<p>Note that this is mainly cargo cult: I have no idea why I need to grab the focus and force a redraw for this to work. Still would be thankful if somebody comes up with a better, explainable solution, because the one I suggest here does not work for more complicated situations, such as GUIs with multiple panels and changing number of elements&#8230;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Daniel Castano		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-400310</link>

		<dc:creator><![CDATA[Daniel Castano]]></dc:creator>
		<pubDate>Wed, 15 Feb 2017 16:59:06 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-400310</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824&quot;&gt;Fabio&lt;/a&gt;.

Same problem here :-( ... so frustrating, as other callbacks as for instance: 
&lt;pre lang=&quot;matlab&quot;&gt;
set(jAxis.getComponent(0),&#039;MouseMovedCallback&#039;,@(x,y)disp(&#039;ab&#039;));
&lt;/pre&gt;

work fine after adding axes to the figure. I really hope that somebody comes up with a solution.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824">Fabio</a>.</p>
<p>Same problem here 🙁 &#8230; so frustrating, as other callbacks as for instance: </p>
<pre lang="matlab">
set(jAxis.getComponent(0),'MouseMovedCallback',@(x,y)disp('ab'));
</pre>
<p>work fine after adding axes to the figure. I really hope that somebody comes up with a solution.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Fabio		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-398824</link>

		<dc:creator><![CDATA[Fabio]]></dc:creator>
		<pubDate>Thu, 19 Jan 2017 17:13:17 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-398824</guid>

					<description><![CDATA[It seem as the callbacks don&#039;t work when an axes is inside the figure (as said also here: https://it.mathworks.com/matlabcentral/answers/284809-focusgainedcallback-doesn-t-work-once-figure-has-axes-2015b ), any solution? Thanks in advance]]></description>
			<content:encoded><![CDATA[<p>It seem as the callbacks don&#8217;t work when an axes is inside the figure (as said also here: <a href="https://it.mathworks.com/matlabcentral/answers/284809-focusgainedcallback-doesn-t-work-once-figure-has-axes-2015b" rel="nofollow ugc">https://it.mathworks.com/matlabcentral/answers/284809-focusgainedcallback-doesn-t-work-once-figure-has-axes-2015b</a> ), any solution? Thanks in advance</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-355565</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 21 Aug 2015 07:19:08 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-355565</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-345515&quot;&gt;Paul Andrews&lt;/a&gt;.

@Paul &amp; @Yvon - in HG2 (R2014b onward), you can simply set the Focus events (FocusGained, FocusLost etc.) on the direct child of the AxisComponent, as follows:

&lt;pre lang=&quot;matlab&quot;&gt;set(jAxis.getComponent(0),&#039;FocusGainedCallback&#039;,{@myMatlabFunc,hFig});&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-345515">Paul Andrews</a>.</p>
<p>@Paul &#038; @Yvon &#8211; in HG2 (R2014b onward), you can simply set the Focus events (FocusGained, FocusLost etc.) on the direct child of the AxisComponent, as follows:</p>
<pre lang="matlab">set(jAxis.getComponent(0),'FocusGainedCallback',{@myMatlabFunc,hFig});</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yvon Halbwachs		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-355558</link>

		<dc:creator><![CDATA[Yvon Halbwachs]]></dc:creator>
		<pubDate>Fri, 21 Aug 2015 06:10:23 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-355558</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-345515&quot;&gt;Paul Andrews&lt;/a&gt;.

Paul, I&#039;m facing the same problem as you have in R2014b. By any chance, did you find any solution to this?

Yvon]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-345515">Paul Andrews</a>.</p>
<p>Paul, I&#8217;m facing the same problem as you have in R2014b. By any chance, did you find any solution to this?</p>
<p>Yvon</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Paul Andrews		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-345515</link>

		<dc:creator><![CDATA[Paul Andrews]]></dc:creator>
		<pubDate>Thu, 12 Feb 2015 18:51:01 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-345515</guid>

					<description><![CDATA[Dear all,

The mechanism described above for detecting windows focus event does not work in R2014b.  I believe the issue is related in changes in the figure’s AxisComponent.  In R2013b, jFrame.getAxisComponent returns:
&lt;pre lang=&quot;matlab&quot;&gt;com.mathworks.hg.peer.FigureAxisComponentProxy$_AxisCanvas[fAxisComponentProxy,0,0,560x420]&lt;/pre&gt;
In R2014b, it returns:
&lt;pre lang=&quot;matlab&quot;&gt;com.mathworks.hg.peer.HeavyweightLightweightContainerFactory$FigurePanelContainerLight[fFigurePanel,0,0,560x420,layout=...]&lt;/pre&gt;
If you have a solution for this problem, please post it.

Paul

&lt;pre lang=&quot;matlab&quot;&gt; 
% Snippet used to test focus callbacks
hFig = figure(&#039;name&#039;, &#039;FocusGainTest&#039;);  % etc. - prepare the figure

% Get the underlying Java reference
warning off MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame
jFrame = get(hFig, &#039;JavaFrame&#039;);
hFrame = handle(jFrame,&#039;CallbackProperties&#039;);

jAxis = jFrame.getAxisComponent 
hAxis = handle(jAxis,&#039;CallbackProperties&#039;);

% Set the focus event callback
set(hAxis,&#039;FocusGainedCallback&#039;, @(a,b) disp(&#039;FocusGainedCallback&#039;));
set(hAxis,&#039;FocusLostCallback&#039;, @(a,b) disp(&#039;FocusLostCallback&#039;));
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Dear all,</p>
<p>The mechanism described above for detecting windows focus event does not work in R2014b.  I believe the issue is related in changes in the figure’s AxisComponent.  In R2013b, jFrame.getAxisComponent returns:</p>
<pre lang="matlab">com.mathworks.hg.peer.FigureAxisComponentProxy$_AxisCanvas[fAxisComponentProxy,0,0,560x420]</pre>
<p>In R2014b, it returns:</p>
<pre lang="matlab">com.mathworks.hg.peer.HeavyweightLightweightContainerFactory$FigurePanelContainerLight[fFigurePanel,0,0,560x420,layout=...]</pre>
<p>If you have a solution for this problem, please post it.</p>
<p>Paul</p>
<pre lang="matlab"> 
% Snippet used to test focus callbacks
hFig = figure('name', 'FocusGainTest');  % etc. - prepare the figure

% Get the underlying Java reference
warning off MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame
jFrame = get(hFig, 'JavaFrame');
hFrame = handle(jFrame,'CallbackProperties');

jAxis = jFrame.getAxisComponent 
hAxis = handle(jAxis,'CallbackProperties');

% Set the focus event callback
set(hAxis,'FocusGainedCallback', @(a,b) disp('FocusGainedCallback'));
set(hAxis,'FocusLostCallback', @(a,b) disp('FocusLostCallback'));
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: SA		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-228990</link>

		<dc:creator><![CDATA[SA]]></dc:creator>
		<pubDate>Wed, 24 Jul 2013 10:41:02 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-228990</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-228966&quot;&gt;SA&lt;/a&gt;.

Solution found - thanks for undoc&#039;d documentation - brilliant.

(1) overload figure.m (place this function in your path)
&lt;pre lang=&#039;matlab&#039;&gt;
%% fix the annoying focus follows mouse bug
%% (c) GPL SA 2013
function h=figure(n);
    global hasfocus;
    if(~exist(&#039;n&#039;))
        n=1;
        end;
    h = builtin(&#039;figure&#039;,n);
    jFig = get(h,&#039;JavaFrame&#039;);
    jAxis = jFig.getAxisComponent;
    set(jAxis,&#039;FocusGainedCallback&#039;,{@setfocus,h});
    hasfocus = h;
&lt;/pre&gt;

(2) catch the mouse focus events (place this function in your path)

&lt;pre lang=&#039;matlab&#039;&gt;
%% fix the annoying focus follows mouse bug
%% (c) GPL SA 2013
function setfocus(jAxis, jEventData, hFig)
    global hasfocus;
    set(0,&#039;currentfigure&#039;,hasfocus)
end
&lt;/pre&gt;

(3) If you are bothered by warning messages spamming up your console put this line in your matlab start up files:

&lt;pre lang=&#039;matlab&#039;&gt;warning off MATLAB:dispatcher:nameConflict&lt;/pre&gt;

Works for me on matlab 2007something]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-228966">SA</a>.</p>
<p>Solution found &#8211; thanks for undoc&#8217;d documentation &#8211; brilliant.</p>
<p>(1) overload figure.m (place this function in your path)</p>
<pre lang='matlab'>
%% fix the annoying focus follows mouse bug
%% (c) GPL SA 2013
function h=figure(n);
    global hasfocus;
    if(~exist('n'))
        n=1;
        end;
    h = builtin('figure',n);
    jFig = get(h,'JavaFrame');
    jAxis = jFig.getAxisComponent;
    set(jAxis,'FocusGainedCallback',{@setfocus,h});
    hasfocus = h;
</pre>
<p>(2) catch the mouse focus events (place this function in your path)</p>
<pre lang='matlab'>
%% fix the annoying focus follows mouse bug
%% (c) GPL SA 2013
function setfocus(jAxis, jEventData, hFig)
    global hasfocus;
    set(0,'currentfigure',hasfocus)
end
</pre>
<p>(3) If you are bothered by warning messages spamming up your console put this line in your matlab start up files:</p>
<pre lang='matlab'>warning off MATLAB:dispatcher:nameConflict</pre>
<p>Works for me on matlab 2007something</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: SA		</title>
		<link>https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-228966</link>

		<dc:creator><![CDATA[SA]]></dc:creator>
		<pubDate>Wed, 24 Jul 2013 09:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=584#comment-228966</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-228963&quot;&gt;SA&lt;/a&gt;.

Can I modify the question - I&#039;d like to capture all focus events for all figures (then I can reset &quot;currentfigure&quot;).  Is there a way to do this?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/detecting-window-focus-events#comment-228963">SA</a>.</p>
<p>Can I modify the question &#8211; I&#8217;d like to capture all focus events for all figures (then I can reset &#8220;currentfigure&#8221;).  Is there a way to do this?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
