jQuery - On escape, close window if overlay not open
<p>When the escape key is pressed, I would like to close a child window, but first check for open overlays. If an overlay is open and the escape key is pressed, I only want the overlay to close. If the user presses escape again, I want it to close the child window.</p>

<p>So, I first need to check whether an overlay is open. All my overlays have a class of <code>.simpleOverlay</code> and a close button with the class <code>.closeOverlayBtn</code>, so I thought of having a function that checks if that class has focus, before closing the window, like so:</p>

<pre><code>if (e.keyCode == 27) { // "Esc" Key
   if ( $('.simpleOverlay').is(':focus') ) {
       $('.closeOverlayBtn').trigger('click');
   }
   else {
       window.close();
   }    
   return(false);
}
</code></pre>

<p>But this is not happening!! If I replace the <code>.trigger</code> line with an <code>alert()</code>, I get alerted when the overlay is closed and open, not good!</p>

<p>Is this the right way to go about this or am I completely missing something?</p>
