Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript:创建键盘事件并随时激活/停用它_Javascript_Typescript_Event Handling_Dom Events_Keyboard Events - Fatal编程技术网

Javascript Typescript:创建键盘事件并随时激活/停用它

Javascript Typescript:创建键盘事件并随时激活/停用它,javascript,typescript,event-handling,dom-events,keyboard-events,Javascript,Typescript,Event Handling,Dom Events,Keyboard Events,我希望能够处理键盘事件,但也能够在代码中按照我的意愿停止/开始再次处理它们 我一直在阅读有关KeyboardEvent,以及它从Event继承的方式的文章,我发现我能够使用一些有用的方法来实现我想要实现的目标,例如: event.initEvent event.preventDefault 但经过一些研究,并试图把事情放在一起,我很挣扎,最终无法找到这样做的方法 为了证明我一直在努力找出解决方案的方法,我想向您展示我迄今为止的成果: class Game { private gameO

我希望能够处理键盘事件,但也能够在代码中按照我的意愿停止/开始再次处理它们

我一直在阅读有关
KeyboardEvent
,以及它从
Event
继承的方式的文章,我发现我能够使用一些有用的方法来实现我想要实现的目标,例如:

  • event.initEvent
  • event.preventDefault
但经过一些研究,并试图把事情放在一起,我很挣扎,最终无法找到这样做的方法

为了证明我一直在努力找出解决方案的方法,我想向您展示我迄今为止的成果:

class Game {

  private gameOver: boolean = false;
  private timeOfRoundMillis: number = 5 * 1000;
  private keyboardEvent: KeyboardEvent;
  private capturedInput: string[];

  constructor() {}

  public play(): void {
    this.round();
  }

  private round(): void {

    if (!this.gameOver) {

      // I want to start capturing keys
      keyboardEvent.initEvent();

      setTimeout(() => {
        // Now I want to stop capturing keys to process the input
        keyboardEvent.preventDefault();

        this.processPlayerInput();
        this.emptyCapturedInput();
        this.round();
      }, this.timeOfRoundMillis);

    }

  }

  private processPlayerInput(): void {
      // do some stuff with 'capturedInput'
  }

  // I want to call this every time a key is pressed if 
  // capturing keyboard events is active
  private capture(e): void {
     capturedInput.push(e.keyPressed);
  }

}
可能事件是“isComposing”(布尔值,如果它正在合成,则为true)
font:

经过合作,我们提出了以下解决方案:

var keypress = require('keypress');

declare var process: any;

class Game {

  private gameOver: boolean = false;
  private timeOfRoundMillis: number = 5 * 1000;
  private capturedInput: string[];

  constructor() {
     keypress(process.stdin);

     process.openStdin().on('keypress', (key) => {
         // I want to call this every time a key is pressed
         this.capturedInput.push(key);
     });
     process.stdin.setRawMode(true);
  }

  public play(): void {
    this.round();
  }

  private round(): void {

    if (!this.gameOver) {

      setTimeout(() => {
        // Now I want to stop capturing keys to process the input
        this.isCapturingKeys = false;  // need to set it to true when you desire to start capturing again
        this.processPlayerInput();
        this.emptyCapturedInput();
        this.round();
      }, this.timeOfRoundMillis);

    } else {
       process.stdin.pause(); // close stdin events
    }

  }

  private processPlayerInput(): void {
      // do some stuff with 'capturedInput'
  }
}

这看起来不错,但我在Node.js上使用的是Typescript,所以
窗口不存在。非常感谢,你让我度过了美好的一天,它工作得非常完美!:-)为了让程序退出,我编辑了解决方案:
process.stdin.pause()丢失