title: Using KeyboardEvent to simulate a keypress for a test
body: <p>I want to write a Javascript test that simulates a keypress on the right arrow key.  Here is my attempt to create the <code>keydown</code> event:</p>
  <pre><code>var data = {
     key: 'ArrowRight'
  };
  var ev = new KeyboardEvent('keydown', data);
  console.log(ev);
  </code></pre>
  <p><a href="http://jsfiddle.net/lsiden/6e7duck6/" rel="nofollow noreferrer">http://jsfiddle.net/lsiden/6e7duck6/</a></p>
  <p>When I examine the output in the developer console, I expect to see the value 39 in one of the event fields, but it appears to remain uninitialized.</p>
  <pre><code>KeyboardEvent {which: 0, keyCode: 0, charCode: 0, repeat: false, metaKey: false…}
  altKey: false
  bubbles: false
  cancelBubble: false
  cancelable: false
  charCode: 0
  ctrlKey: false
  currentTarget: null
  defaultPrevented: false
  detail: 0
  eventPhase: 0
  keyCode: 0
  keyIdentifier: &quot;&quot;
  keyLocation: 0
  layerX: 0
  layerY: 0
  location: 0
  metaKey: false
  pageX: 0
  pageY: 0
  path: Array[0]
  repeat: false
  returnValue: true
  shiftKey: false
  srcElement: null
  target: null
  timeStamp: 1430931169397
  type: &quot;keydown&quot;
  view: null
  which: 0
  __proto__: KeyboardEvent
  .
  </code></pre>
  <p>I tried to follow the docs: <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent</a></p>
  <p>Has anyone done this before?</p>