简体   繁体   English

Node.JS Readline在Backspace键上打印新行

[英]Node.JS Readline prints a new line on backspace keypress

I have some code to read in some user input, nothing fancy: 我有一些代码可以在一些用户输入中读取,没什么花哨的:

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Please enter some text:\n', function(answer){
    rl.pause();
    console.log(answer);
});

Unfortunately, when the user is prompted for the input, if they make a typo and attempt to use BACKSPACE to delete the text, the prompt is shifted down a line. 不幸的是,当提示用户输入时,如果他们输入错误并尝试使用BACKSPACE删除文本,则提示会向下移动一行。 Is there a way to stop this from happening? 有没有办法阻止这种情况的发生?

After looking into this further, it appears it only occurs if the rl.question() prompt contains a \\n . 在进一步研究之后,它似乎仅在rl.question()提示符包含\\n If the prompt doesn't contain a newline character, the text can be backspaced safely. 如果提示中不包含换行符,则可以安全地将文本退格。

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Please enter some text: ', function(answer){
    rl.pause();
    console.log(answer);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM