Reputation: 3617
Without making use of a data
event, I want this code to log the unicode reference of each key as I press it down. I can't understand why I'm getting Null
everytime.
Whenever I press down a key on my keyboard I'll fire a readable event on process.stdin
running a callback which allows me to read data from this readable stream. So why it isn't holding any data from my keypresses?
// nodo.js
function nodo() {
var stdin = process.stdin;
var stdout = process.stdout;
if (stdin.isTTY) {
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdin.resume();
stdout.write('\u000A>Bienvenido\u000A');
} else {
process.exit();
}
stdin.on('readable', function(){
var input = stdin.read();
console.log(input);
});
}
nodo();
I appreciate your attention.
Upvotes: 2
Views: 1916
Reputation: 24812
Please have a read of that document
that explains how to correctly handle process.stdin
. Your mistake was to use stdin.resume
that enables the "old" compatibility mode on process stdin streams.
// nodo.js
function nodo() {
var stdin = process.stdin;
var stdout = process.stdout;
if (stdin.isTTY) {
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdout.write('\u000A>Bienvenido\u000A');
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write('data: ' + chunk);
}
});
process.stdin.on('end', function() {
process.stdout.write('end');
});
} else {
process.exit();
}
}
nodo();
Upvotes: 1