Keypress Event Emitter for Node.js Streams
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
-
Error: require() is not defined in ES module scope
cause Attempting to use the CommonJS `require()` statement in a Node.js project configured to use ES Modules (e.g., via `"type": "module"` in `package.json` or `.mjs` files).fixConvert your project to CommonJS, or use a dynamic import workaround if absolutely necessary: `const keypress = await import('keypress'); keypress.default(process.stdin);`. However, migrating to `readline.emitKeypressEvents` is strongly recommended for ESM projects. -
TypeError: keypress is not a function
cause The `keypress` module's default export is a function, not an object with methods. This error occurs if you try to access properties (e.g., `keypress.someMethod()`) on the imported `keypress` function.fixCall `keypress` directly as a function with your `ReadableStream` (e.g., `keypress(process.stdin)`) to enable keypress events. The events are then emitted on the stream itself, not the `keypress` object. -
No 'keypress' event emitted on `process.stdin`
cause The `keypress` module was imported but its main function was not called with `process.stdin` to activate the event emitter, or `process.stdin.setRawMode(true)` was not enabled.fixEnsure both `keypress(process.stdin);` and `process.stdin.setRawMode(true); process.stdin.resume();` are called in your script before attaching an event listener to `process.stdin`.
Warnings
- breaking This package was designed primarily for Node.js v0.8.x and earlier versions. Its behavior and necessity are highly inconsistent or deprecated in modern Node.js environments (Node.js 10+).
- gotcha The `keypress` function must be explicitly called with a `ReadableStream` (e.g., `process.stdin`) to initiate keypress event emission. Merely requiring the module does not activate the functionality.
- deprecated The package is unmaintained, with its last release (0.2.1) dating back over a decade (circa 2012-2013). Using abandoned software introduces significant security risks due to unpatched vulnerabilities and lack of modern best practices.
- gotcha To receive individual keypress events without waiting for the Enter key, the input stream (e.g., `process.stdin`) must be set to raw mode using `process.stdin.setRawMode(true)`.
Install
-
npm install keypress -
yarn add keypress -
pnpm add keypress
Imports
- keypress
import keypress from 'keypress';
const keypress = require('keypress'); - keypress function call
const handler = new keypress.Handler();
keypress(process.stdin);
- keypress type (TypeScript)
import type { Keypress } from 'keypress';import keypress = require('keypress');
Quickstart
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