Gra Gra - 10 months ago 75
Javascript Question

How can use "getModifierState" on focus event with JS?

I'm trying to get caps lock status on focus event of input this is my code

$('#Input')[0].addEventListener('focus', function (key) {
if (key.originalEvent.getModifierState("CapsLock"))
//do something...
else {
//do something..

But i'm getting this error "Cannot read property 'getModifierState' of undefined", and I don't know what I'm doing wrong.
Some advice or link for to read thanks in advance.

Answer Source

There are different types of event object. getModifierState is only defined for KeyboardEvent (such as a keydown) and MouseEvent (such as a click); focus is neither.

You might look at using click and remembering whether you already had focus (e.g., so you don't repeat the action if the click is in an already-focussed element). Ugh, no, that's horrible. There are lots of ways your element might get focus other than clicks.