title: Show searchbar and focus it after keypress
body: <p>I want to show a searchbar when the user presses the "R" key, the searchbar should also be focused after the key press, here's the fiddle:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>buttonKeyAllowed = true;
  
  function disparitionMenu() 
  /* this function is used to show the searchbar */
  {
      var input = jQuery("#mod-search-searchword");
      input[0].selectionStart = input[0].selectionEnd = input.val().length;
      jQuery("#menu-search").css("display", "inline-block");
  }
      
  /*-- key events --*/
  
  
  jQuery(document).keyup(function() {
    buttonKeyAllowed = true;
  });
  
  
  jQuery(document).focus(function(e) { 
    buttonKeyAllowed = true;
  });
  
  
  jQuery(document).keypress(function(e) {  
         
    if (e.repeat != undefined) {
      buttonKeyAllowed = !e.repeat;
    }
    if (!buttonKeyAllowed) 
      return;
    buttonKeyAllowed = false;
  
  
    var code = (e.keyCode || e.which);
  
    if(code == 114) {
      disparitionMenu();
    }
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#menu-search
  {
    display: none;
  }
  
  input{
    width: 100px;
    background-color: white;
   }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  
  
    &lt;div id="megamenu"&gt;
      &lt;div id="menu-search"&gt;
        &lt;input type="text" id="mod-search-searchword" /&gt;
      &lt;/div&gt;
    &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This code works nice, but there's one problem:</p>
  
  <p>When the user presses the "R" key a little bit too long, 'r' is written in the searchbar, and I want to avoid it.</p>
  
  <p>What's the cleanest way to correct this problem? I don't want to add a <code>while(/* is R key down*/){}</code> loop before the function <code>disparitionMenu()</code></p>
  
  <p>Thank you for your help.</p>