nodejs how to read keystrokes from stdin

Inputnode.jsStdin

Input Problem Overview


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?

Input Solutions


Solution 1 - Input

For those finding this answer since this capability was stripped from tty, here's how to get a raw character stream from stdin:

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 );
});

pretty simple - basically just like process.stdin's documentation but using setRawMode( true ) to get a raw stream, which is harder to identify in the documentation.

Solution 2 - Input

You can achieve it this way, if you switch to raw mode:
var stdin = process.openStdin(); 
require('tty').setRawMode(true);    

stdin.on('keypress', function (chunk, key) {
  process.stdout.write('Get Chunk: ' + chunk + '\n');
  if (key && key.ctrl && key.name == 'c') process.exit();
});

Solution 3 - Input

In node >= v6.1.0:

const readline = require('readline');

readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

process.stdin.on('keypress', (str, key) => {
  console.log(str)
  console.log(key)
})

See https://github.com/nodejs/node/issues/6626

Solution 4 - Input

This version uses the keypress module and supports node.js version 0.10, 0.8 and 0.6 as well as iojs 2.3. Be sure to run npm install --save keypress.

var keypress = require('keypress')
  , tty = require('tty');

// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);

// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
  console.log('got "keypress"', key);
  if (key && key.ctrl && key.name == 'c') {
    process.stdin.pause();
  }
});

if (typeof process.stdin.setRawMode == 'function') {
  process.stdin.setRawMode(true);
} else {
  tty.setRawMode(true);
}
process.stdin.resume();

Solution 5 - Input

With nodejs 0.6.4 tested (Test failed in version 0.8.14):

rint = require('readline').createInterface( process.stdin, {} ); 
rint.input.on('keypress',function( char, key) {
    //console.log(key);
    if( key == undefined ) {
    	process.stdout.write('{'+char+'}')
    } else {
    	if( key.name == 'escape' ) {
	    	process.exit();
	    }
	    process.stdout.write('['+key.name+']');
    }

}); 
require('tty').setRawMode(true);
setTimeout(process.exit, 10000);

if you run it and:

  <--type '1'
{1}
  <--type 'a'
{1}[a]

Important code #1:

require('tty').setRawMode( true );

Important code #2:

.createInterface( process.stdin, {} );

Solution 6 - Input

if(process.stdout.isTTY){
  process.stdin.on("readable",function(){
    var chunk = process.stdin.read();
    if(chunk != null) {
      doSomethingWithInput(chunk);
    }
  });
  process.stdin.setRawMode(true);
} else {
  console.log("You are not using a tty device...");
}

Solution 7 - Input

Based on Dan Heberden's answer, here's an async function -

async function getKeypress() {
  return new Promise(resolve => {
    var stdin = process.stdin
    stdin.setRawMode(true) // so get each keypress
    stdin.resume() // resume stdin in the parent process
    stdin.once('data', onData) // like on but removes listener also
    function onData(buffer) {
      stdin.setRawMode(false)
      resolve(buffer.toString())
    }
  })
}

Use like so -

console.log("Press a key...")
const key = await getKeypress()
console.log(key)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbanticView Question on Stackoverflow
Solution 1 - InputDan HeberdenView Answer on Stackoverflow
Solution 2 - InputDanSView Answer on Stackoverflow
Solution 3 - Inputarve0View Answer on Stackoverflow
Solution 4 - InputPeter LyonsView Answer on Stackoverflow
Solution 5 - InputbefzzView Answer on Stackoverflow
Solution 6 - InputLuxView Answer on Stackoverflow
Solution 7 - InputBrian BurnsView Answer on Stackoverflow