title: Onclick element simulate Esc key
body: <p>I have the following situation:</p>
  
  <p>When the user clicks on a link, through Javascript Esc key should be triggered. 
  So to be able to explain it better I have the following
  code so far:</p>
  
  <p>1.I've created a function:</p>
  
  <pre><code>//my function
  function invokeEscKey() {
      var ev = document.createEvent('KeyboardEvent');
      ev.initKeyEvent(
          'keydown', true, true, window, false, false, false, false, 27, 0);
      document.body.dispatchEvent(ev);
  }
  </code></pre>
  
  <p>also tried this with jQuery:</p>
  
  <pre><code>//my function
  function invokeEscKey() {
      jQuery.event.trigger({
          type : 'keypress', 
          which : 27 
      });
  }
  </code></pre>
  
  <p>2.And when the user clicks on the href, the function should be called:</p>
  
  <pre><code>$(function() {
      $('a.close').click(function() {
          //call this function to simulate ESC key press
          invokeEscKey();
      });
  });
  </code></pre>
  
  <p>But this doesn't work at all. My question is, what is the best way to solve this?</p>
  
  <p>Note: The reason I ask this is because I have a popup with the F11 key (full screen mode) is activated. When the user clicks on the close button, the F11 must be undone, which is normally done by pressing on the ESC key. And I want this to be done automatically.</p>