Last active
March 22, 2021 14:14
-
-
Save jasongaylord/7797819 to your computer and use it in GitHub Desktop.
A few JavaScript functions to detect if caps lock is on or not. More can be read about this at:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Test Caps Lock</title> | |
</head> | |
<body> | |
<div id="capsLockWarning" style="font-weight: bold; color: maroon; margin: 0 0 10px 0; display: none;">Caps Lock is on.</div> | |
Username: <input type="text" id="username" /><br/> | |
Password: <input type="password" id="password" /> | |
</body> | |
<script language="javascript"> | |
function isCapsLockOn(e) { | |
var keyCode = e.keyCode ? e.keyCode : e.which; | |
var shiftKey = e.shiftKey ? e.shiftKey : ((keyCode == 16) ? true : false); | |
return (((keyCode >= 65 && keyCode <= 90) && !shiftKey) || ((keyCode >= 97 && keyCode <= 122) && shiftKey)) | |
} | |
function showCapsLockMsg(e) { | |
var warningElement = document.getElementById('capsLockWarning'); | |
if (isCapsLockOn(e)) | |
warningElement.style.display = 'block'; | |
else | |
warningElement.style.display = 'none'; | |
} | |
document.onkeypress = function (e) { | |
e = e || window.event; | |
showCapsLockMsg(e); | |
} | |
</script> | |
</html> |
@jasongaylord: Even if we check if the entered character is a capital letter or not, doesn't actually mean that the CapsLock is ON. As We can just press Shift + any char to add a capital letter anyways.
It's not respective to the specific Android devices.
Please check this out: Stackoverflow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@aniruddhashevle So in that case, I'd probably recommend falling back to performing a regex check on the string entered and look for all caps. Unfortunately, mobile/software keyboards do not always have keypress listeners. Out of curiosity, is it all keyboards or a specific one like the stock Android or Samsung's?