检测页面加载时的大写锁定状态(或类似)
在我的研究中,我发现了如何在按下大写键时检测大写锁定。但是,即使在未触摸按键的情况下,我也想知道大写状态。
例子: alert(ui.Keyboard.capslock) // 将返回 true 或 false;
谢谢!
In my research, I've found how to detect capslock when the caps key is pressed. However, I want to know the caps status even in cases when the key is not touched.
example:
alert(ui.Keyboard.capslock) // would return true or false;
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您无法在页面加载时获取键盘按钮的状态。您必须分析按键的按键代码。这是唯一的办法。
No, you cannot get the state of a keyboard button on page load. You have to analyze a keyCode of a keypress. That is the only way.
我刚刚提出了一种替代方案,它将检测大写锁定的状态并将其存储,以便在按下大写锁定键时可以打开和关闭警告。我只针对 chrome 45+ 和 ie9+ 进行编码,因此如果这是您的计划,可能需要对一般用途进行一些调整。
这是我的 html:
这是 js:
我使用命名空间来避免
isDetected
和capsOn
的全局变量,因此在一些之前的LOGINPAGE.CAPSDetect.
函数和变量。请参阅 this jsfiddle 了解无命名空间并对其进行测试。I've just come up with an alternative which will detect the state of caps lock and store it so that if the caps lock key is pressed a warning can be turned on and off. I'm only coding for chrome 45+ and ie9+ so there may need to be some adjustments for general use if that is your plan.
Here's my html:
And here's the js:
I'm using namespaces to avoid globals for
isDetected
andcapsOn
hence theLOGINPAGE.CAPSdetect.
before some functions and variables. See this jsfiddle for no namespacing and to test it out.试试这个代码:
Try this code: