相关疑难解决方法(0)

nodejs如何从stdin读取键击

是否可以在运行的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)

我正在寻找一个与例如红宝石相当的nodejsgetc

这可能吗?

stdin input node.js

105
推荐指数
6
解决办法
8万
查看次数

标签 统计

input ×1

node.js ×1

stdin ×1