0

It is necessary to simulate the keystrokes in the input. Yes, I know that I can change data via input, but this does not suit me.

I tried through KeyboardEvents, but it doesn’t work.

const ke = new KeyboardEvent("keydown", {
    bubbles: true, cancelable: true, keyCode: 50
});

document.body.dispatchEvent(ke);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • What if you add the key down event on the document.body. eg. document.body.addEventListener('keydown', callbackFunction) – manish Feb 21 '20 at 07:56
  • Can you elaborate on why do you need to simulate keyboard events? Maybe you just need to insert text in a way that more or less preserve the ctrl + z history: https://stackoverflow.com/questions/11076975/how-to-insert-text-into-the-textarea-at-the-current-cursor-position – Andre Pena Feb 21 '20 at 07:58
  • The keystroke itself occurs, but no characters are added in the input. I need this for my extension for Chrome to change the data in one input site. The site has an unusual input, when I try to change the data through the .value nothing happens. Only keyboard input works. Sorry my bad english – Ragirhid Feb 21 '20 at 08:15

0 Answers0