How can use “getModifierState” on focus event with JS?

落花浮王杯 提交于 2019-12-25 08:58:07

问题


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 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 a terrible suggestion. There are lots of ways your element might get focus other than clicks.



来源:https://stackoverflow.com/questions/40156072/how-can-use-getmodifierstate-on-focus-event-with-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!