import { emitKeypressEvents } from 'readline'; const handlers: Map = new Map(); export function on(keyName: string, callback: Function) { handlers.set(keyName, callback); } export function init() { emitKeypressEvents(process.stdin); (process.stdin as any).setRawMode(true); process.stdin.on('keypress', (letter, key) => { if (!key) return; if (key.ctrl && (key.name === 'c' || key.name === 'd')) { console.log('bye bye'); process.exit(); } for (const [keyName, callback] of handlers.entries()) { if (key.name === keyName) { callback(); } } }); }