UNPKG

lib-curses

Version:

Simple node.js library for work with console

41 lines (35 loc) 1.05 kB
import * as readline from 'readline'; /** * Reads a single key press from standard input and returns it as a buffer. * * This function enables raw mode for standard input, allowing it to capture * key presses immediately without waiting for Enter key to be pressed. * * @async * @function getch * @returns {Promise<Buffer>} A promise that resolves with a buffer containing pressed key. * * @example * (async () => { * const key = await getch(); * console.log(`You pressed: ${key.toString()}`); * })(); */ export async function getch(): Promise<Buffer> { return new Promise((resolve) => { const { stdin } = process; stdin.setRawMode(true); const rl: any = readline.createInterface({ input: stdin, output: undefined, terminal: false, }); const handleKeyPress = (key: Buffer) => { stdin.setRawMode(false); rl.input.off('data', handleKeyPress); rl.close(); resolve(key); }; rl.input.on('data', handleKeyPress); }); }