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 input is buffered until the next newline, like so:
// stdin_test.js
var stdin = process.openStdin();
stdin.on('data', function(chunk) { console.log("Got chunk: " + chunk); });
Running this, I get:
$ node stdin_test.js
<-- type '1'
<-- type '2'
<-- hit enter
Got chunk: 12
What I'd like is to see:
$ node stdin_test.js
<-- type '1' (without hitting enter yet)
Got chunk: 1
I'm looking for a nodejs equivalent to, e.g., getc
in ruby
Is this possible?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…