How to implement to detecting and warning users when caps lock is on with (or not) tooltip style in typescript (angular 4.2.2)?? Maybe with some keyup events, or like toUpperCase()
in JS.

- 27,856
- 27
- 95
- 103

- 648
- 3
- 18
- 34
-
Possible duplicate of [How do you tell if caps lock is on using JavaScript?](https://stackoverflow.com/questions/348792/how-do-you-tell-if-caps-lock-is-on-using-javascript) – Den Isahac Jul 11 '17 at 13:11
-
1@Den Isahac I think thats different from JS and TS – Eduard Arevshatyan Jul 24 '17 at 12:59
4 Answers
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);
}
}

- 27,856
- 27
- 95
- 103
-
i would to show message when caps lock is on, maybe do it without any styles? – Eduard Arevshatyan Jul 13 '17 at 10:35
-
Detect CapsLock on click, keyup and keydown on current window. No need to add any event in html doc
import { Component, OnInit, HostListener } from '@angular/core';
export class LoginComponent implements OnInit {
constructor(){}
ngOnInit() {}
@HostListener('window:click', ['$event']) onClick(event){
if (event.getModifierState && event.getModifierState('CapsLock')) {
this.capslockOn = true;
} else {
this.capslockOn = false;
}
}
@HostListener('window:keydown', ['$event'])
onKeyDown(event){
if (event.getModifierState && event.getModifierState('CapsLock')) {
this.capslockOn = true;
} else {
this.capslockOn = false;
}
}
@HostListener('window:keyup', ['$event'])
onKeyUp(event){
if (event.getModifierState && event.getModifierState('CapsLock')) {
this.capslockOn = true;
} else {
this.capslockOn = false;
}
}
}

- 2,321
- 23
- 18
-
Is it possible to just show the message when the field is touched? – CptDayDreamer May 03 '19 at 03:47
-
as of now, Its not possible to detect in smartphones using only HTML/JS – Gopala Raja Naika Aug 14 '19 at 05:40
-
1
I don't think Angular can do this out of the box (AngularJS can).
There are a few libraries that can do this though, it would be worth checking out capsLock (NPM, GitHub)
You are able to use an observable to check if the caps lock is enabled and then do you custom stuff
capsLock.observe(function (result) {
// Do stuff
});
Here is the example from the Github readme: https://rawgit.com/aaditmshah/capsLock/master/demo.html

- 7,973
- 2
- 45
- 83
@HostListener('window:click', ['$event'])
@HostListener('window:keydown', ['$event'])
@HostListener('window:keyup', ['$event'])
onCapsCheck(event: MouseEvent | KeyboardEvent) {
this.capsOn = !!(event.getModifierState && event.getModifierState('CapsLock'));
}

- 201
- 2
- 8