So I've been trying to get the highest possible score on the Typing test on HumanBenchmark but ran into some oddities when trying to dispatch custom keybaordEvents.
I only got it working if I captured user inputs and then dispatched them again, but I don't understand why creating my own events won't work. HumanBenchmark don't seem to be checking the isTrusted flag since that is false in both scenarios.
Working code:
let vTarget = document.getElementsByClassName("letters")[0]
let vKeyEvents = {}
document.addEventListener("keydown",
function(e){
vKeyEvents[e.key] = e
}
);
function run(){
vText = vTarget.innerText
for (let index = 0; index < vText.length; index++) {
vTarget.dispatchEvent(vKeyEvents[vText[index]])
}
}
How to run my code (Demonstration):
vTest = new KeyboardEvent('keydown',
{
key:"t",
keyCode:84,
isComposing: true,
view:window
});
document.getElementsByClassName("letters")[0].dispatchEvent(vTest)
Add bubbles: true
to the KeyboardEvent
options
new KeyboardEvent('keydown',
{
key:"t",
keyCode:84,
isComposing: true,
view:window,
bubbles: true
})