问题
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.
回答1:
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 Ugh, no, that's a terrible suggestion. There are lots of ways your element might get focus other than clicks.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).
来源:https://stackoverflow.com/questions/40156072/how-can-use-getmodifierstate-on-focus-event-with-js