15 ŞUBAT 2011, Salı
okumayı nodejs standart girdiden tuş vuruşlarını
Nodejs çalışan bir komut dosyası içinde gelen tuş vuruşlarını dinlemek mümkün mü?
Eğer 'data'
olayını process.openStdin()
ve dinle o zaman kullanırsam giriş gibi bir sonraki satır kadar tamponlu,:
// stdin_test.js
var stdin = process.openStdin();
stdin.on('data', function(chunk) { console.log("Got chunk: " chunk); });
Bu çalışan, anlıyorum:
$ node stdin_test.js
<-- type '1'
<-- type '2'
<-- hit enter
Got chunk: 12
İstiyorum ne olduğunu görmek için
$ node stdin_test.js
<-- type '1' (without hitting enter yet)
Got chunk: 1
Bir nodejs, örneğin, getc
in ruby eşdeğer arıyorum
Bu mümkün mü?
CEVAP
20 EYLÜL 2012, PERŞEMBE
Bu yeteneği tty
, şeritli beri bu cevabı bulmak için buraya standart girdiden ham karakter bir akış almak için nasıl:
var stdin = process.stdin;
// without this, we would only get streams once enter is pressed
stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();
// i don't want binary, do you?
stdin.setEncoding( 'utf8' );
// on any data into stdin
stdin.on( 'data', function( key ){
// ctrl-c ( end of text )
if ( key === '\u0003' ) {
process.exit();
}
// write the key to stdout all normal like
process.stdout.write( key );
});
oldukça basit, temelde sadece process.stdin's documentation gibi ama setRawMode( true )
belgelerinde tanımlamak için daha zor olan doğal bir akış elde etmek için kullanarak.
Bunu Paylaş:
Sıkıştırma standart girdiden veri okun...
Python: standart girdiden Okuma parola...
Neden Standart girdiden okuma satır Py...
Nasıl satır satır standart girdiden ok...
Nasıl Python standart girdiden okur mu...