Created
          August 23, 2016 19:17 
        
      - 
      
 - 
        
Save albulescu/29d587a55e1237b75673e39dc6399f76 to your computer and use it in GitHub Desktop.  
    Listen for passwords while on page
  
        
  
    
      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 { 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