Keypress Event Emitter for Node.js Streams

0.2.1 · abandoned · verified Sun Apr 19

The `keypress` package (version 0.2.1) is a JavaScript utility designed to make any Node.js `ReadableStream` emit "keypress" events. It was created to provide this functionality independently, particularly for Node.js `v0.8.x` where the `keypress` event on `process.stdin` was either undocumented or only emitted when used with the `readline` module. This module extracts and provides that specific logic. Released around 2012, this package is considered abandoned and is not maintained for modern Node.js environments. While it historically filled a gap for console applications requiring direct input handling, current Node.js versions offer built-in alternatives through the `readline` module's `emitKeypressEvents` method. It addresses a specific legacy need for low-level terminal input without relying on the full `readline` interface.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to enable and listen for "keypress" events on `process.stdin`, handling basic input and a Ctrl+C exit condition.

const keypress = require('keypress');

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

// enable raw mode for direct key input without waiting for Enter
if (process.stdin.isTTY) {
  process.stdin.setRawMode(true);
}

// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
  console.log('got "keypress" event:', ch, key);
  if (key && key.ctrl && key.name === 'c') {
    console.log('Ctrl+C pressed. Exiting.');
    process.stdin.pause(); // Stop listening for input
    process.exit(); // Terminate the process
  }
});

console.log('Press any key (Ctrl+C to exit)');
process.stdin.resume(); // Start listening for input

view raw JSON →