nodejs how to read keystrokes from stdin

前端 未结 6 1831
忘掉有多难
忘掉有多难 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:10

    You can achieve it this way, if you switch to raw mode:

    var stdin = process.openStdin(); 
    require('tty').setRawMode(true);    
    
    stdin.on('keypress', function (chunk, key) {
      process.stdout.write('Get Chunk: ' + chunk + '\n');
      if (key && key.ctrl && key.name == 'c') process.exit();
    });
    

提交回复
热议问题