Last active
December 8, 2021 06:35
-
-
Save faisalahmedador/febc8360b82ccdc024912fbc4601c941 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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