暂停脚本,直到用户在 Node.JS 中按下回车键

Pause script until user presses enter in Node.JS

我有一个脚本,里面有一点信息。我希望它在用户按下回车键之前不做任何事情。我试过这个:

console.log("Welcome to the Roblox cursor backup script!\n This script will backup your current Roblox cursor and replace it with the old 2021 Roblox cursor.\n\n Please make sure you have a Roblox client closed. \n This script is prefixed at /Applications/Roblox.app, if you are non-admin please run the command with the --noadmin flag.\n\n Press enter to continue. \n\n ---------------------------------------------------------");
// if user presses enter in the terminal, continue
process.stdin.on('keypress', function (letter, key) {
    if (key.name === 'return') {
        console.log("User pressed enter/return, continuing...");
    } else{
        // pause the script until the user presses enter
        process.stdin.pause();
    }

});

但是,脚本立即继续。如何使脚本在用户按下 enter/return 之前不执行任何操作?

完整源代码:https://github.com/Link2Linc/Old-Roblox-Cursor/blob/master/main.ts

您可以简单地从标准输入中获取输入 示例函数:

function waitForKey(keyCode) {
    return new Promise(resolve => {
        process.stdin.on('data',function (chunk) {
            if (chunk[0] === keyCode) {
                resolve();
                process.stdin.pause();
            }
        });
    });
}

现在,如果您想等待输入密钥:

await waitForKey(10);