nodejs how to read keystrokes from stdin

前端 未结 6 1794
忘掉有多难
忘掉有多难 2020-11-28 01:37

Is it possible to listen for incoming keystrokes in a running nodejs script? If I use process.openStdin() and listen to its \'data\' event then the

6条回答
  •  一整个雨季
    2020-11-28 02:07

    if(Boolean(process.stdout.isTTY)){
      process.stdin.on("readable",function(){
        var chunk = process.stdin.read();
        if(chunk != null)
          doSomethingWithInput(chunk);
      });
      process.stdin.setRawMode(true);
    } else {
      console.log("You are not using a tty device...);
    }
    

提交回复
热议问题