{"id":11210,"library":"keypress","title":"Keypress Event Emitter for Node.js Streams","description":"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.","status":"abandoned","version":"0.2.1","language":"javascript","source_language":"en","source_url":"git://github.com/TooTallNate/keypress","tags":["javascript","keypress","readline","core"],"install":[{"cmd":"npm install keypress","lang":"bash","label":"npm"},{"cmd":"yarn add keypress","lang":"bash","label":"yarn"},{"cmd":"pnpm add keypress","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only, as it was released prior to widespread ESM adoption in Node.js.","wrong":"import keypress from 'keypress';","symbol":"keypress","correct":"const keypress = require('keypress');"},{"note":"The default export is a function that must be called with a ReadableStream to enable keypress events. It does not expose a class or other properties.","wrong":"const handler = new keypress.Handler();","symbol":"keypress function call","correct":"keypress(process.stdin);"},{"note":"For TypeScript, a CommonJS-style import is required. There are no built-in type definitions, and `@types/keypress` refers to a different library. The package lacks official TypeScript support.","wrong":"import type { Keypress } from 'keypress';","symbol":"keypress type (TypeScript)","correct":"import keypress = require('keypress');"}],"quickstart":{"code":"const keypress = require('keypress');\n\n// make `process.stdin` begin emitting \"keypress\" events\nkeypress(process.stdin);\n\n// enable raw mode for direct key input without waiting for Enter\nif (process.stdin.isTTY) {\n  process.stdin.setRawMode(true);\n}\n\n// listen for the \"keypress\" event\nprocess.stdin.on('keypress', function (ch, key) {\n  console.log('got \"keypress\" event:', ch, key);\n  if (key && key.ctrl && key.name === 'c') {\n    console.log('Ctrl+C pressed. Exiting.');\n    process.stdin.pause(); // Stop listening for input\n    process.exit(); // Terminate the process\n  }\n});\n\nconsole.log('Press any key (Ctrl+C to exit)');\nprocess.stdin.resume(); // Start listening for input","lang":"javascript","description":"This example demonstrates how to enable and listen for \"keypress\" events on `process.stdin`, handling basic input and a Ctrl+C exit condition."},"warnings":[{"fix":"For modern Node.js applications, use `readline.emitKeypressEvents(process.stdin)` and `process.stdin.setRawMode(true)` from the built-in `readline` module instead.","message":"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+).","severity":"breaking","affected_versions":">=1.0.0 (Node.js versions)"},{"fix":"Ensure you call `keypress(yourReadableStream)` after requiring the module to enable events.","message":"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.","severity":"gotcha","affected_versions":"*"},{"fix":"Migrate to Node.js's native `readline` module for keypress event handling or consider a actively maintained alternative like `emit-keypress` if additional features are needed.","message":"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.","severity":"deprecated","affected_versions":"*"},{"fix":"Add `process.stdin.setRawMode(true);` and `process.stdin.resume();` to your script before listening for keypress events. Remember to handle `process.exit()` or `process.stdin.pause()` to exit raw mode gracefully.","message":"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)`.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Convert 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.","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).","error":"Error: require() is not defined in ES module scope"},{"fix":"Call `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.","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.","error":"TypeError: keypress is not a function"},{"fix":"Ensure 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`.","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.","error":"No 'keypress' event emitted on `process.stdin`"}],"ecosystem":"npm"}