<ul dir="auto">
<li>Electron version: electron-prebuilt@0.36.12</li>
<li>Operating system: MAC OS X Yosemite 10.10.5</li>
</ul>
<p dir="auto">I have this sample setup:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const BrowserWindow = electron.BrowserWindow;
// ... rest of electron boilerplate

function createMainWindow() {
    const win = new electron.BrowserWindow({
        width: 600,
        height: 400
    });

    win.on('blur', blurHandler);
    win.on('focus', blurHandler);

    win.loadURL(`file://${__dirname}/index.html`);
    win.on('closed', onClosed);

    return win;
}

function blurHandler() {
    let focusedWindow = BrowserWindow.getFocusedWindow();
    if (focusedWindow) {
        console.log('There is a focused window!');
    } else {
        console.log('no focused window. :(');
    }
}"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-v">BrowserWindow</span> <span class="pl-c1">=</span> <span class="pl-s1">electron</span><span class="pl-kos">.</span><span class="pl-c1">BrowserWindow</span><span class="pl-kos">;</span>
<span class="pl-c">// ... rest of electron boilerplate</span>

<span class="pl-k">function</span> <span class="pl-en">createMainWindow</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-s1">win</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-s1">electron</span><span class="pl-kos">.</span><span class="pl-c1">BrowserWindow</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
        <span class="pl-c1">width</span>: <span class="pl-c1">600</span><span class="pl-kos">,</span>
        <span class="pl-c1">height</span>: <span class="pl-c1">400</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'blur'</span><span class="pl-kos">,</span> <span class="pl-s1">blurHandler</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'focus'</span><span class="pl-kos">,</span> <span class="pl-s1">blurHandler</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">loadURL</span><span class="pl-kos">(</span><span class="pl-s">`file://<span class="pl-s1"><span class="pl-kos">${</span><span class="pl-s1">__dirname</span><span class="pl-kos">}</span></span>/index.html`</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">'closed'</span><span class="pl-kos">,</span> <span class="pl-s1">onClosed</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-k">return</span> <span class="pl-s1">win</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-en">blurHandler</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> <span class="pl-s1">focusedWindow</span> <span class="pl-c1">=</span> <span class="pl-v">BrowserWindow</span><span class="pl-kos">.</span><span class="pl-en">getFocusedWindow</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">focusedWindow</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'There is a focused window!'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
        <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">'no focused window. :('</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">The above code simply calls <code class="notranslate">blurHandler()</code> on the <code class="notranslate">blur</code> and <code class="notranslate">focus</code> events.</p>
<p dir="auto"><strong>The expected behavior:</strong> is to get the console output of "<code class="notranslate">There is a focused window!</code>" when I click back (to get focus) on the only Electron window, and to get the output of "<code class="notranslate">no focused window. :(</code>" when I click on any other application windows, or otherwise trigger the <code class="notranslate">blur</code> event on the electron window.</p>
<p dir="auto"><strong>The actual behavior:</strong>  No matter the method of gaining focus on the electron window I can never get the <code class="notranslate">BrowserWindow.getFocusedWindow()</code> method to return anything except <code class="notranslate">undefined</code>. My log just looks like this:</p>
<pre class="notranslate">no focused window. :(
no focused window. :(
no focused window. :(
no focused window. :(
no focused window. :(
no focused window. :(
</pre>
<p dir="auto">Can anyone see what I am doing wrong here, or have I found a bug?</p>
<p dir="auto">Hi,</p>
<p dir="auto">I'm working on a new feature in our App which relate with focus behaviour. The feature is to capture if an atom-shell app is totally lost focused. Since my App can have multiple windows, I can not simply do this by using one window's <code class="notranslate">blur</code> and <code class="notranslate">focus</code> events. Instead, I try to do this by detecting if all of my windows get unfocused when a <code class="notranslate">blur</code> event fired.</p>
<p dir="auto">Here is my script to do this:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    this.nativeWin.on ( 'blur', function () {
        if ( !BrowserWindow.getFocusedWindow() ) {
            /// none of my atom-shell windows get focused, safely send 'totally-blur' message here!
        }
    }.bind(this) );"><pre class="notranslate">    <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">nativeWin</span><span class="pl-kos">.</span><span class="pl-en">on</span> <span class="pl-kos">(</span> <span class="pl-s">'blur'</span><span class="pl-kos">,</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">if</span> <span class="pl-kos">(</span> <span class="pl-c1">!</span><span class="pl-v">BrowserWindow</span><span class="pl-kos">.</span><span class="pl-en">getFocusedWindow</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-c">/// none of my atom-shell windows get focused, safely send 'totally-blur' message here!</span>
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span><span class="pl-kos">.</span><span class="pl-en">bind</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">)</span> <span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Actually the above script works correctly in Mac OSX, but when I test it in Windows platform, the <code class="notranslate">getFocusedWindow()</code> will still return the window who fired <code class="notranslate">blur</code> there. What I expect is a null/undefined here.</p>
<h3 dir="auto">Another suggestion:</h3>
<p dir="auto">I think the <code class="notranslate">focus</code> and <code class="notranslate">blur</code> event can carry more information from its event. The event should known which window get lost and which one get focus to (if that is an atom-shell window). This is very similar to what we have in DOM event. The <code class="notranslate">focus-in</code>, <code class="notranslate">focus-out</code>, <code class="notranslate">focus</code> and <code class="notranslate">blur</code> event have a parameter named event.relatedTarget, which will holding the next focus or last blur element, and will become null/undefined if user just click mouse outside browser.</p>
<p dir="auto">Besides, like what we discussed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="53607634" data-permission-text="Title is private" data-url="https://github.com/electron/electron/issues/981" data-hovercard-type="issue" data-hovercard-url="/electron/electron/issues/981/hovercard" href="https://github.com/electron/electron/issues/981">#981</a> , if we have the ability to detect DevTools' window behaviour, that would be perfect for detecting totally blur in my case.</p>
<h3 dir="auto">Test environment:</h3>
<p dir="auto">I test this under Windows 7/8, Mac OSX 10.10.1 with atom-shell v0.20.4.</p>