Skip to content

Instantly share code, notes, and snippets.

@faisal6699
Last active December 8, 2021 06:35
Show Gist options
  • Save faisal6699/febc8360b82ccdc024912fbc4601c941 to your computer and use it in GitHub Desktop.
Save faisal6699/febc8360b82ccdc024912fbc4601c941 to your computer and use it in GitHub Desktop.
import {Directive, OnInit, HostListener, Output, EventEmitter} from "@angular/core";
@Directive({
selector: '[capsLockDetect]'
})
export class CapsLockDetectDirective implements OnInit{
@Output() capsLockOn = new EventEmitter<number>();
capslock:number = 0;
constructor(){}
ngOnInit() {
}
@HostListener('keyup', ['$event'])
onKeyUp(event: KeyboardEvent): void {
if (event.key !== 'CapsLock') {
if (event.getModifierState('CapsLock')) {
this.capsLockOn.emit(1)
this.capslock = 1;
} else {
this.capsLockOn.emit(2);
this.capslock = 2;
}
} else if (event.key === 'CapsLock' && this.capslock !== 0) {
if (this.capslock === 1) {
this.capsLockOn.emit(2);
this.capslock = 2;
} else {
this.capsLockOn.emit(1);
this.capslock = 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment