{"id":20206775,"url":"https://github.com/enquirer/emit-keypress","last_synced_at":"2025-10-09T08:39:43.261Z","repository":{"id":251745165,"uuid":"838350561","full_name":"enquirer/emit-keypress","owner":"enquirer","description":"Drop-dead simple keypress event emitter for Node.js. Create powerful CLI applications and experiences with ease.","archived":false,"fork":false,"pushed_at":"2025-02-05T08:32:38.000Z","size":141,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-19T23:37:44.815Z","etag":null,"topics":["bin","binding","chord","cli","enquirer","keybinding","keyboard","keycode","keymap","keypress","prompt","readline","sequence","shortcut","terminal"],"latest_commit_sha":null,"homepage":"https://github.com/jonschlinkert","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enquirer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":"support/emit-keys.ts","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-05T13:13:04.000Z","updated_at":"2025-03-09T13:59:59.000Z","dependencies_parsed_at":"2024-09-13T06:12:44.572Z","dependency_job_id":"4575438b-056c-419a-b6b7-fc6dd3d7971d","html_url":"https://github.com/enquirer/emit-keypress","commit_stats":null,"previous_names":["enquirer/emit-keypress"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Femit-keypress","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Femit-keypress/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Femit-keypress/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Femit-keypress/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enquirer","download_url":"https://codeload.github.com/enquirer/emit-keypress/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217126,"owners_count":21066633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bin","binding","chord","cli","enquirer","keybinding","keyboard","keycode","keymap","keypress","prompt","readline","sequence","shortcut","terminal"],"created_at":"2024-11-14T05:26:00.102Z","updated_at":"2025-10-09T08:39:43.174Z","avatar_url":"https://github.com/enquirer.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# emit-keypress [![NPM version](https://img.shields.io/npm/v/emit-keypress.svg?style=flat)](https://www.npmjs.com/package/emit-keypress) [![NPM monthly downloads](https://img.shields.io/npm/dm/emit-keypress.svg?style=flat)](https://npmjs.org/package/emit-keypress) [![NPM total downloads](https://img.shields.io/npm/dt/emit-keypress.svg?style=flat)](https://npmjs.org/package/emit-keypress)\n\n\u003e Drop-dead simple keypress event emitter for Node.js. Create powerful CLI applications and experiences with ease.\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save emit-keypress\n```\n\n## Why another keypress module?\n\nNode's built-in `readline` module is great for handling user input if you need something simple. But when using `createInterface`, there are a number of built-in behaviors that are difficult or impossible to override when you need more control over the input stream.\n\nOther keypress modules I found either did too much, or didn't allow for enough customization. This module is designed to be simple, flexible, easy to use, and easy to customize.\n\n## Why use this?\n\n**Create your own CLI interface**\n\nIt's lightweight, no dependencies, easy to use, and easy to customize. It's designed to be used in a wide range of use-cases, from simple command-line utilities to complex terminal applications.\n\nPowerful CLI applications can be built with this module.\n\n## Usage\n\n```js\nimport { emitKeypress } from 'emit-keypress';\n\nemitKeypress({ input: process.stdin });\n\nprocess.stdin.on('keypress', (input, key) =\u003e {\n  console.log({ input, key });\n\n  if (input === '\\x03' || input === '\\r') {\n    process.stdin.pause();\n  }\n});\n```\n\n## API\n\n### onKeypress\n\nPass an `onKeypress` function to `emitKeypress` to handle keypress events.\n\n```ts\n// The \"close\" function is passed as the third argument\nconst onKeypress = async (input, key, close) =\u003e {\n  // do stuff with keypress events\n  console.log({ input, key });\n\n  // Close the stream if the user presses `Ctrl+C` or `Enter`\n  if (input === '\\x03' || input === '\\r') {\n    close();\n  }\n};\n\nemitKeypress({ onKeypress });\n```\n\nA `close` function is also returned from `emitKeypress` that can be called to close the stream.\n\n```ts\nconst { close } = emitKeypress({ onKeypress });\n\n// close the stream\nsetTimeout(() =\u003e {\n  close();\n}, 10_000);\n```\n\n### keymap\n\nPass a `keymap` array to map keypress events to specific shortcuts.\n\n```ts\nemitKeypress({\n  keymap: [\n    { sequence: '\\x03', shortcut: 'ctrl+c' },\n    { sequence: '\\r', shortcut: 'return' }\n  ],\n  onKeypress: async (input, key, close) =\u003e {\n    // do stuff with keypress events\n    console.log({ input, key });\n\n    if (key.shortcut === 'return' || key.shortcut === 'ctrl+c') {\n      close();\n    }\n  }\n});\n```\n\nNote that you can add arbitrary properties the keymap objects. This is useful for mapping shortcuts to commands.\n\n**Example**\n\n```ts\nemitKeypress({\n  keymap: [\n    { sequence: '\\x1B', shortcut: 'escape', command: 'cancel' },\n    { sequence: '\\x03', shortcut: 'ctrl+c', command: 'cancel' },\n    { sequence: '\\r', shortcut: 'return', command: 'submit' }\n  ],\n  onKeypress: async (input, key, close) =\u003e {\n    // do stuff with keypress events\n    switch (key.command) {\n      case 'cancel':\n        console.log('canceled');\n        close();\n        break;\n\n      case 'submit':\n        console.log('submitted');\n        break;\n    }\n  }\n});\n```\n\n### input\n\nPass a `ReadableStream` to `input` to listen for keypress events on the stream.\n\n```ts\nemitKeypress({\n  input: process.stdin,\n  onKeypress: async (input, key, close) =\u003e {\n    // do stuff with keypress events\n    console.log({ input, key });\n\n    if (key.shortcut === 'return' || key.shortcut === 'ctrl+c') {\n      close();\n    }\n  }\n});\n```\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Author\n\n**Jon Schlinkert**\n\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n\n### License\n\nCopyright © 2025, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the MIT License.\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on February 05, 2025._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenquirer%2Femit-keypress","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenquirer%2Femit-keypress","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenquirer%2Femit-keypress/lists"}