Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript readline:当用户输入一个字母时,我希望立即带着该字母退出_Javascript_Node.js_Module_Node Modules_Readline - Fatal编程技术网

javascript readline:当用户输入一个字母时,我希望立即带着该字母退出

javascript readline:当用户输入一个字母时,我希望立即带着该字母退出,javascript,node.js,module,node-modules,readline,Javascript,Node.js,Module,Node Modules,Readline,我使用的是Nodejs,readline模块,我要做的是当用户输入一封信时。他们输入的键我想马上让那个键(字母)返回,就像按下回车键一样,但仍然是他们按下的键(字母) 在下面的代码中,我有一个提示,要求用户输入w、a、s或d,因此我希望在单击时立即返回这4个键,如果他们输入任何其他键,则让终端忽略它,而不显示它。我该怎么做 const readline = require('readline').createInterface({ input: process.stdin, output: pr

我使用的是Nodejs,readline模块,我要做的是当用户输入一封信时。他们输入的键我想马上让那个键(字母)返回,就像按下回车键一样,但仍然是他们按下的键(字母)

在下面的代码中,我有一个提示,要求用户输入w、a、s或d,因此我希望在单击时立即返回这4个键,如果他们输入任何其他键,则让终端忽略它,而不显示它。我该怎么做

const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
/// the function is below
function userInput(input) {
  input = readline.question('Please move\n (w for up)\n (a for left)\n (s for down)\n (d for right)', 
  theMove => {
  console.log(`Nice move ${theMove}!`);
  readline.close();
});
userInput();

您可以这样做:


const readline = require('readline').createInterface({
   input: process.stdin,
   output: process.stdout
   });
   /// the function is below
   function userInput() {
   const allowedKeys = 'wasd';
     input = readline.question('Please move\n (w for up)\n (a for left)\n (s for down)\n (d for right)', 
     theMove => {
        if (allowedKeys.includes(theMove)) {
         console.log(`Nice move ${theMove}!`);
         readline.close();
        }
        else {
         userInput();
         console.log('\n');
         console.log(`Nice move ${theMove}!`);
        }
   });
}
userInput();
var keypress = require('keypress')
  , tty = require('tty');

// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);

// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
   
  console.log('got "keypress"', key.name);
  
  const allowedKeys = 'wasd';

  if (allowedKeys.includes(key.name)) {
    process.exit();
  }
});

if (typeof process.stdin.setRawMode == 'function') {
  process.stdin.setRawMode(true);
} else {
  tty.setRawMode(true);
}
process.stdin.resume();
如果用户按下除允许之外的任何键,它将再次调用userInput

这来自Node.js readline文档:

每当输入流接收到 行结束输入(\n、\r或\r\n)。这通常发生在用户 按Enter或Return

这就是它的工作原理。它将在接收到行尾输入时调用调用,否则将继续等待输入。检查

而是使用从用户处获取输入,如下所示:


const readline = require('readline').createInterface({
   input: process.stdin,
   output: process.stdout
   });
   /// the function is below
   function userInput() {
   const allowedKeys = 'wasd';
     input = readline.question('Please move\n (w for up)\n (a for left)\n (s for down)\n (d for right)', 
     theMove => {
        if (allowedKeys.includes(theMove)) {
         console.log(`Nice move ${theMove}!`);
         readline.close();
        }
        else {
         userInput();
         console.log('\n');
         console.log(`Nice move ${theMove}!`);
        }
   });
}
userInput();
var keypress = require('keypress')
  , tty = require('tty');

// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);

// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
   
  console.log('got "keypress"', key.name);
  
  const allowedKeys = 'wasd';

  if (allowedKeys.includes(key.name)) {
    process.exit();
  }
});

if (typeof process.stdin.setRawMode == 'function') {
  process.stdin.setRawMode(true);
} else {
  tty.setRawMode(true);
}
process.stdin.resume();

查看更多详细信息。

我已经用switch语句完成了这项操作,但我希望当他们按“a”时,功能基本上在按下时退出,我希望它立即执行,他们基本上不必按enter键,然后当他们按任何其他字母时,我甚至不希望它们被打印或显示,因此它们被删除完全是。希望这有意义您应该检查readline的工作情况,因为首先了解任何模块的工作情况,然后期望从中获得o/p是很重要的。若你们已经做了一些和switch语句一起工作的事情,那个就太棒了。这有帮助吗?或者你们还有问题吗?