按任意键继续在nodejs中
我需要一个函数来暂停脚本的执行,直到按下一个键。我试过了:
var stdin = process.openStdin();
require('tty').setRawMode(true);
stdin.on('keypress', function (chunk, key) {
process.stdout.write('Get Chunk: ' + chunk + '\n');
if (key && key.ctrl && key.name == 'c') process.exit();
});
但它只是在听按键而没有任何反应。该程序不会继续执行。
我该如何暂停执行?
回答如下:适合我:
console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));