Note: This question is not asking about how to obtain the character from a keyEvent.
Suppose there is a <input type="text"/>
element, and a series of keyEvents fired on that element. These events can be triggered by any keys, including characters, arrows and backspaces. This results in a string which can be obtained via the value
property of that input element. Now the question is, how to obtain this string without the input element?
The reason why the input element can't be directly used is because these events are already fired on a focused element. I tried to create an input element and use dispatchEvent()
to reproduce the effect, but this doesn't work due to the reason described here. String.fromCharCode()
also won't work as the effect of arrow keys and backspaces will not be reproduced.