我正在努力:
这是我的功能:
export async function inkey_promise() {
console.log('Press any key...');
return new Promise((resolve) => {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
const listener = (str, readline_key) => {
process.stdin.setRawMode(false);
process.stdin.resume();
console.log(`RETURNING: `, readline_key);
return resolve(readline_key);
};
process.stdin.once('keypress', listener);
});
}
我这样称呼它:
const key_info = await inkey_promise();
问题:
按键后:
process.exit()
,因为按下按键后它总是会做一些事情......但是当它没有什么可做的时候,它仍然应该像以前一样正常退出。所以看来我还需要做一些其他事情才能将进程/标准输入/键盘完全返回到其默认行为/状态?
import readline from 'readline';
type Args_tui_prompt_inkey = {
echo_debug: boolean;
};
export async function tui_prompt_inkey(args?: Args_tui_prompt_inkey): Promise<Output_tui_prompt_inkey> {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const listener = (_arg1: never, rkey: readline.Key) => {
rl.close();
if (args?.echo_debug) console.log(`rkey is: `, rkey);
resolve({
rkey: rkey,
});
};
// using ['input'] to suppress TS errors
rl['input'].once('keypress', listener);
rl['input'].setRawMode(true);
rl['input'].resume();
});
}
type Output_tui_prompt_inkey = {
rkey: readline.Key;
};
rl.input
rl['input']
来代替。