Learn how to find out if capslock is on inside an input field with JavaScript.
Try to press the "Caps Lock" key inside the input field:
WARNING! Caps lock is ON.
// Get the input field
var input = document.getElementById("myInput");
// Get the warning text
var text = document.getElementById("text");
// When the user presses any key on the keyboard, run the function
input.addEventListener("keyup", function(event) {
// If "caps lock" is pressed, display the warning text
if (event.getModifierState("CapsLock")) {
text.style.display = "block";
} else {
text.style.display = "none"
}
});
Try it Yourself »
Tip: Read more about the getModifierState() method in our JavaScript Event Reference - getModifierState().
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!