getModifierState() not working on dispatched Event

102 Views Asked by At

I'm trying to check for capslock state on page load. So far I've made this event listener:

document.addEventListener("keydown", capslockCheck)

and I'm dispatching an event on page load:

const event = new KeyboardEvent("keydown", { key: "Tab" })  // the key doesn't matter
document.dispatchEvent(event);

The event listener catches the event correctly but when I try to call getModifierState("CapsLock") on this simulated event, it always returns false. Whereas when a real key is pressed, it properly returns true | false as it should.

Thanks for your time guys!

0

There are 0 best solutions below