JS to simulate key down

Hi -
Is it possible to have javascript mimic a key down action. For example, I’d like to have a button when clicked to enter a certain letter into a text field.

I found this but could not get it to work:

 var e = new Event("keydown");
  e.key="a";    // just enter the char you want to send 
  e.keyCode=e.key.charCodeAt(65);
  e.which=e.keyCode;
  e.altKey=false;
  e.ctrlKey=true;
  e.shiftKey=false;
  e.metaKey=false;
  e.bubbles=true;
  document.dispatchEvent(e);