Skip to content

Instantly share code, notes, and snippets.

@newvertex
Created September 17, 2016 10:24
Show Gist options
  • Save newvertex/d78b9c6050d6a8f830809e6e528d5e96 to your computer and use it in GitHub Desktop.
Save newvertex/d78b9c6050d6a8f830809e6e528d5e96 to your computer and use it in GitHub Desktop.
Example: Detect keypress event in Node.js console app
var readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY)
process.stdin.setRawMode(true);
process.stdin.on('keypress', (chunk, key) => {
if (key && key.name == 'q')
process.exit();
});
@etherealHero
Copy link

๐Ÿ‘

@tfrank11
Copy link

๐Ÿ

@stevemu
Copy link

stevemu commented Oct 24, 2024

Thank you!

@5aurabh
Copy link

5aurabh commented Mar 29, 2025

Thanks for this. Is there a standard way to detect a keyrelease event or do we need to rely on some timeout logic which tracks inactivity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment