I just ran into a problem. I wanted to write a small script that wrote into an input field by simulating key presses. But it doesn't work.
<input type="text" id="mainInput" />
const inputElement = document.getElementById("mainInput");
for (let i = 0; i < 100; i++) {
const newEvent = new KeyboardEvent("keypress", {
charCode: 115,
key: "s",
which: 115
});
inputElement.dispatchEvent(newEvent);
}
I also attached an event listener and could confirm that my event is triggered correctly. I assume it is because the isTrusted
flag is false
and I understand that I cannot change that when I create an event via javascript.
But I skimmed now twice over the documetation of the input and text input field on MDN. In it has no mention of that. Because of that I cannot be sure if it really is the isTrusted
flag or if I do something wrong. I am pretty convinced of it, but that's still different from having definite confirmation.
So, my question is two-fold:
1. Is it the isTrusted
flag why it isn't working?
2. Where could I have read about it? Did I overlook it on MDN? Or is it hidden in some obscure RFC?