nodejs how to read keystrokes from stdin

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

    In node >= v6.1.0:

    const readline = require('readline');
    
    readline.emitKeypressEvents(process.stdin);
    process.stdin.setRawMode(true);
    
    process.stdin.on('keypress', (str, key) => {
      console.log(str)
      console.log(key)
    })
    

    See https://github.com/nodejs/node/issues/6626

提交回复
热议问题