title: How can I simulate a keypress in JavaScript?
body: <p>I'm trying to find a way to simulate a keypress.</p>
  
  <p>For example, when function launched, the key "Arrow Down" should be pressed and so the webpage should be slightly scrolled.</p>
  
  <p>I'm interested only in Chrome, and both jQuery or plain JS will be appropriate. (Plain JS will be more preferable).</p>
  
  <p>That's one of the code examples I tried:</p>
  
  <pre><code>var e = $.Event("keydown", { keyCode: 40}); // 40 = down arrow
  $("body").trigger(e);
  // When I launch it the console, nothing happens. The page is not scrolled.
  // May be I missed some obvious?
  </code></pre>
  
  <p>I searched and found the following related questions, but the solutions did not work for me:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery">Definitive way to trigger keypress events with jQuery</a></li>
  <li><a href="https://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript">Firing a Keyboard Event in JavaScript</a></li>
  <li><a href="https://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript">How to trigger event in JavaScript?</a></li>
  <li><a href="https://stackoverflow.com/questions/5516600/simulate-left-and-right-arrow-key-event-with-javascript">Simulate left and right arrow key event with javascript</a></li>
  <li><a href="https://stackoverflow.com/questions/1468384/simulate-keypress-with-jquery">Simulate Keypress With jQuery</a></li>
  <li><a href="https://stackoverflow.com/questions/15722096/simulating-a-keypress-event-from-javascript-console">Simulating a Keypress Event from Javascript Console</a></li>
  <li><a href="https://stackoverflow.com/questions/596481/simulate-javascript-key-events">Simulate JavaScript Key Events</a></li>
  </ul>
  
  <hr>
  
  <p><strong>In other words</strong></p>
  
  <p>Using AutoHotkey, you can easily make something like:</p>
  
  <pre><code>Down::
  Send, {Up}
  </code></pre>
  
  <p>Then, if you press <code>Down</code> arrow key, it will be triggered <code>Up</code>. I just want to implement it with JS.</p>