I am trying to detect whether the client has their CapsLock enabled by temporarily creating a hidden input field, triggering a keyup
event on that input, and then checking the boolean value of e.getModifierState('CapsLock')
.
The function I've written is below, which currently throws the error Uncaught TypeError: e.getModifierState is not a function
on the line which includes e.getModifierState
and I'm struggling to diagnose why this is occurring. I'm open to any help here!
let checkCaps = () => {
let tempInput = document.createElement('input');
let keyupEvent = new Event('keyup');
tempInput.type = 'hidden';
tempInput.id = 'check-caps';
let capsOn = false;
tempInput.addEventListener('keyup', e => {
e.which = 65;
capsOn = e.getModifierState('CapsLock');
})
tempInput.dispatchEvent(keyupEvent);
tempInput.remove();
return capsOn;
}
checkCaps(); // testing the function