What is Readline.emitKeypressEvents() in Node.js?

Readline.emitKeyPressEvents() makes the given Readable stream start emitting keypress events corresponding to the provided input.

Protoype

readline.emitKeypressEvents(stream[, interface])

Parameters

  • stream <stream.Readable>: The Readable stream from which the keypress event will be emitted.
  • interface<readline.Interface>: (optional) Specifies the interface for which autocompletion is turned off.

Code

const readline = require('readline');

console.log("Press any key")

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

process.stdin.on('keypress', (str, key) => {
    if(key.ctrl == true && key.name == 'c'){
        process.exit()
    }
    console.log(str)
    console.log(key)
})

The code above demonstrates basic use of Readline.emitKeyPressEvents(). The code prints out the pressed key and its attributes to the standard output. To quit the program, press ctrl + c.

Note: If the stream is TTY, then it should be set to RAW mode.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved