How do you tell if caps lock is on using JavaScript?
You can give it a try.. Added a working example. When focus is on input, turning on caps lock makes the led go red otherwise green. (Haven't tested on mac/linux)
NOTE: Both versions are working for me. Thanks for constructive inputs in the comments.
OLD VERSION: https://jsbin.com/mahenes/edit?js,output
Also, here is a modified version (can someone test on mac and confirm)
NEW VERSION: https://jsbin.com/xiconuv/edit?js,output
NEW VERSION:
function isCapslock(e) {
const IS_MAC = /Mac/.test(navigator.platform);
const charCode = e.charCode;
const shiftKey = e.shiftKey;
if (charCode >= 97 && charCode <= 122) {
capsLock = shiftKey;
} else if (charCode >= 65 && charCode <= 90
&& !(shiftKey && IS_MAC)) {
capsLock = !shiftKey;
}
return capsLock;
}
OLD VERSION:
function isCapslock(e) {
e = (e) ? e : window.event;
var charCode = false;
if (e.which) {
charCode = e.which;
} else if (e.keyCode) {
charCode = e.keyCode;
}
var shifton = false;
if (e.shiftKey) {
shifton = e.shiftKey;
} else if (e.modifiers) {
shifton = !!(e.modifiers & 4);
}
if (charCode >= 97 && charCode <= 122 && shifton) {
return true;
}
if (charCode >= 65 && charCode <= 90 && !shifton) {
return true;
}
return false;
}
For international characters, additional check can be added for the following keys as needed. You have to get the keycode range for characters you are interested in, may be by using a keymapping array which will hold all the valid use case keys you are addressing...
uppercase A-Z or 'Ä', 'Ö', 'Ü',
lowercase a-Z or 0-9 or 'ä', 'ö', 'ü'
The above keys are just sample representation.
How can i check if caps lock is ON?
you can check like this using event.getModifierState("CapsLock")
:-
var inputVal = document.getElementById("customInput");var textMsg = document.getElementById("warningDiv");
inputVal.addEventListener("keyup", function(event) {
if (event.getModifierState("CapsLock")) { textMsg.style.display = "block"; } else { textMsg.style.display = "none" }});
#warningDiv{ color: red; display: none;}
<input type="text" id="customInput" />
<p id="warningDiv">WARNING! Caps lock is ON.</p>
Detecting when CAPS LOCK is ON
$("input[type='password']").keypress(function(e) {
var $warn = $(this).next(".capsWarn");//can be removed since you are just using alert
var kc = e.which; //get keycode
var isUp = (kc >= 65 && kc <= 90) ? true : false; // uppercase
var isLow = (kc >= 97 && kc <= 122) ? true : false; // lowercase
// event.shiftKey does not seem to be normalized by jQuery(?) for IE8-
var isShift = ( e.shiftKey ) ? e.shiftKey : ( (kc == 16) ? true : false ); // shift is pressed
// uppercase w/out shift or lowercase with shift == caps lock
if ( (isUp && !isShift) || (isLow && isShift) ) {
capLock(); // alerts "CAPSLOCK is ON"
}
});
function capLock() {
alert('CAPSLOCK is ON');
}
How do I detect the CAPS LOCK state in a password field
You can find a decent example of how to do this here: http://www.codeproject.com/KB/scripting/Detect_Caps_Lock.aspx
You could take that code and wrap it into a different kind of event, either, i.e. onfocus or on document load.
You can google for an index of key codes (I'd post a link, but I don't have high enough karma to post more than one link, sorry).
For simplicity the codes you'd be looking for is 20 (Caps lock).
Instructions copied here from site (license: CPOL)
Building the script
<script language="Javascript">
function capLock(e){
kc = e.keyCode?e.keyCode:e.which;
sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
document.getElementById('divMayus').style.visibility = 'visible';
else
document.getElementById('divMayus').style.visibility = 'hidden';
}
</script>
Now we have our script ready to go. We now need to associate this script with our form.
Using the script
Let's add two items: a textbox and a DIV. Now we just need to call the onKeyPress event.
<input type="password" name="txtPassword" onkeypress="capLock(event)" />
<div id="divMayus" style="visibility:hidden">Caps Lock is on.</div>
Detect and warn users about caps lock is on
Write a directive and add a listener. Add it to your component's main wrapper div, so component would get the emits. As soon it receives the event change, trigger the state of a property linked to a label tag. It will help to hide and show with *ngIf, the condition being the response from your listener (via an @Output to the component).
The following displays a message dynamically:
HTML:
<div (capsLock)="capsOn=$event">
<input type="text" >
<label *ngIf="capsOn">Caps Locked</label>
</div>
Directive:
@Directive({ selector: '[capsLock]' })
export class TrackCapsDirective {
@Output('capsLock') capsLock = new EventEmitter<Boolean>();
@HostListener('window:keydown', ['$event'])
onKeyDown(event: KeyboardEvent): void {
const capsOn = event.getModifierState && event.getModifierState('CapsLock');
this.capsLock.emit(capsOn);
}
@HostListener('window:keyup', ['$event'])
onKeyUp(event: KeyboardEvent): void {
const capsOn = event.getModifierState && event.getModifierState('CapsLock');
this.capsLock.emit(capsOn);
}
}
DEMO
How do you tell if caps lock is on using JavaScript?
You can give it a try.. Added a working example. When focus is on input, turning on caps lock makes the led go red otherwise green. (Haven't tested on mac/linux)
NOTE: Both versions are working for me. Thanks for constructive inputs in the comments.
OLD VERSION: https://jsbin.com/mahenes/edit?js,output
Also, here is a modified version (can someone test on mac and confirm)
NEW VERSION: https://jsbin.com/xiconuv/edit?js,output
NEW VERSION:
function isCapslock(e) {
const IS_MAC = /Mac/.test(navigator.platform);
const charCode = e.charCode;
const shiftKey = e.shiftKey;
if (charCode >= 97 && charCode <= 122) {
capsLock = shiftKey;
} else if (charCode >= 65 && charCode <= 90
&& !(shiftKey && IS_MAC)) {
capsLock = !shiftKey;
}
return capsLock;
}
OLD VERSION:
function isCapslock(e) {
e = (e) ? e : window.event;
var charCode = false;
if (e.which) {
charCode = e.which;
} else if (e.keyCode) {
charCode = e.keyCode;
}
var shifton = false;
if (e.shiftKey) {
shifton = e.shiftKey;
} else if (e.modifiers) {
shifton = !!(e.modifiers & 4);
}
if (charCode >= 97 && charCode <= 122 && shifton) {
return true;
}
if (charCode >= 65 && charCode <= 90 && !shifton) {
return true;
}
return false;
}
For international characters, additional check can be added for the following keys as needed. You have to get the keycode range for characters you are interested in, may be by using a keymapping array which will hold all the valid use case keys you are addressing...
uppercase A-Z or 'Ä', 'Ö', 'Ü',
lowercase a-Z or 0-9 or 'ä', 'ö', 'ü'
The above keys are just sample representation.
Related Topics
Script Tag in JavaScript String
Sending Emails with JavaScript
Making a JavaScript String SQL Friendly
HTML Input Type=File, Get the Image Before Submitting the Form
What Is the Correct Way to Write HTML Using JavaScript
Change Text Color Based on Brightness of the Covered Background Area
Creating Dynamic Button with Click Event in JavaScript
Html5 Drag and Drop Anywhere on the Screen
Property 'Submit' of Object #<Htmlformelement> Is Not a Function
How to Efficiently Randomly Select Array Item Without Repeats
JavaScript Get Xpath of a Node
Axios Posting Params Not Read by $_Post
How to Manually Trigger Validation with Jquery Validate
How to Select Element That Does Not Have Specific Class
Make Anchor Link Go Some Pixels Above Where It's Linked To
Where Should I Put the CSS and JavaScript Code in an HTML Webpage