是否可以在运行的nodejs脚本中侦听传入的击键?如果我使用process.openStdin()并听取它的'data'事件,那么输入将被缓冲,直到下一个换行符,如下所示:
// stdin_test.js
var stdin = process.openStdin();
stdin.on('data', function(chunk) { console.log("Got chunk: " + chunk); });
Run Code Online (Sandbox Code Playgroud)
运行这个,我得到:
$ node stdin_test.js
<-- type '1'
<-- type '2'
<-- hit enter
Got chunk: 12
Run Code Online (Sandbox Code Playgroud)
我想要看的是:
$ node stdin_test.js
<-- type '1' (without hitting enter yet)
Got chunk: 1
Run Code Online (Sandbox Code Playgroud)
这可能吗?