-1

This is a more specific question of Is it possible to simulate key press events programmatically? Every answer is missing the following information in one way or another, reducing the question's value. I've tried three things on there already that didn't work in modern browsers, or were advised against by official sources.

Here is what I'm looking for:

  1. At the time of answering, every example should work in modern versions of Firefox and Chrome.
  2. No examples include deprecated objects, fields, properties, or functions.
  3. Every answer should include a way of typing a printable character in a textfield and textarea. As mentioned in a comment below, this can't be done.
  4. Every answer should include a way to type an alphanumeric character when an input does not have focus. e.g., "a", "b", "c", etc.
  5. Every answer should include a way to type a non-printable character. e.g., page down, left arrow, the F1 key (if this isn't possible, it's okay to state so), Enter, etc.
  6. Every answer should include a link to all the "codes" needed to simulate alphanumeric characters and non-printable characters, or even better, embed that info into the answer. I'm using the term "codes" loosely here: I mean whatever term is appropriate to to satisfy the 2nd point.
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    Artificial key events won't put text into a text field. You have to set the value directly. https://stackoverflow.com/a/50219991/691711 can you also enumerate the 3 things that you already tried that didn't work? – zero298 Jun 20 '21 at 21:06
  • So... not asking for much then? Just want someone else to go through all of the standards and browser-specific quirks sites out there and compile that for you? Most of this is available on [MDN's page for `KeyboardEvent`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent) and the linked event-specific pages. – Heretic Monkey Jun 20 '21 at 21:10
  • Three clicks off of that page got me to [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values), which lists all of the codes. – Heretic Monkey Jun 20 '21 at 21:18
  • 1
    @HereticMonkey If you are saying it's easy to find, how did you find it? I'm not being lazy, I looked before I asked this question. There are literally 200 links on that first page alone. – Daniel Kaplan Jun 20 '21 at 21:21
  • I knew I wanted values for codes. I clicked the `KeyboardEvent.code` property, knowing that most documentation lists valid values for a property in the property documentation. On that page was a header ["Code values"](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code#code_values). The subsequent paragraph had a link to the page. – Heretic Monkey Jun 21 '21 at 11:33

2 Answers2

2

This solution fills all of the criteria:

  1. This example works in modern versions of chrome and firefox (https://caniuse.com/dispatchevent) (https://caniuse.com/keyboardevent-key)
  2. This example does not include anything deprecated
  3. N/A
  4. Alphanumeric characters are supported
  5. This is possible with this solution
  6. You can set key equal to one of these values https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values (I would embed this, however, there are a lot of codes)
  • Ctrl: Use the ctrlKey option (boolean)
  • Alt: Use the altKey option (boolean)
  • Shift: Use the shiftKey option (boolean)
  • Meta Key/Command Key: use the metaKey option (boolean)

function simulateKeypress(target, options) {
  target.dispatchEvent(new KeyboardEvent('keydown', options));
}

document.addEventListener("keydown", event => {
  console.log(event.key);
});

 
setTimeout(() => {

  simulateKeypress(document, {
    key: "F1"
  });
}, 1000);
Ameer
  • 1,980
  • 1
  • 12
  • 24
-1

(As already stated on the linked original question, it is unclear what "simulate" means here. However:)

We maintain a package for simulating user events for testing at @testing-library/user-event.

This includes userEvent.type() API for conveniently simulating keyboard input to input elements as well as userEvent.keyboard() API for simulating keyboard events anywhere. The library simulates the common browser behavior. It will dispatch the key events, change element values, move selection and focus - dependent on the pressed keys and possibly custom event handler that e.g. stop propagation.

The primary target audience for the library is testing with jest and jsdom, but other environments including testing in real browsers are also supported.

(For examples and further explanations see the linked docs. For missing features, bugs, etc.: Issues and PRs are always welcome at the linked Github repo :) )