Skip to content

Instantly share code, notes, and snippets.

@albulescu
Created August 23, 2016 19:17
Show Gist options
  • Save albulescu/29d587a55e1237b75673e39dc6399f76 to your computer and use it in GitHub Desktop.
Save albulescu/29d587a55e1237b75673e39dc6399f76 to your computer and use it in GitHub Desktop.
Listen for passwords while on page
import { Injectable, EventEmitter } from '@angular/core';
@Injectable()
export class TypeingListen extends EventEmitter<boolean> {
private keyStack:string[] = [];
private emptyInterval:any;
constructor( private password:String ) {
super();
window.addEventListener('keyup', this.onKeyDown.bind(this));
}
private cleanStack():void {
this.keyStack.length = 0;
}
private validate():void {
if (this.keyStack.join('') === this.password.toUpperCase() ) {
this.emit(true);
}
}
private onKeyDown( event:KeyboardEvent ):void {
this.keyStack.push(
String.fromCharCode(event.keyCode)
);
this.validate();
if (this.emptyInterval) {
clearTimeout(this.emptyInterval);
}
this.emptyInterval = setTimeout(this.cleanStack.bind(this), 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment