How JavaScript detects the status of uppercase lock keys
This article mainly introduces the relevant knowledge of how JavaScript detects the status of uppercase locking keys, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this JavaScript article on how to detect the status of uppercase locking keys. Let's take a look.
HTML:
Username: Password: Caps Lock is on
JavaScript:
Const el = document.getElementById ('password'); const msg = document.getElementById (' password-message'); el.addEventListener ('keyup', e = > {msg.style = e.getModifierState (' CapsLock')? 'display: block':' display: none';})
As you can see from the above example, the 'keyup' event is used on the selected element, and then KeyboardEvent.getModifierState () is called and the state of the' CapsLock' key is checked. Keydown' and 'keypress' can play a similar role. But after testing on multiple devices, it seems that the keyup method should be preferred because it works better on different operating systems and browsers.
This is the end of the article on "how JavaScript detects the status of uppercase lock keys". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to detect the status of uppercase lock keys in JavaScript". If you want to learn more, you are welcome to follow the industry information channel.