0

I am using Nodejs the readline module what I am trying to do is when the user inputs a letter. The key they input I want to straight away have that key(letter) to be returned like as if it was the enter key being pressed but still be the key(letter) they pressed.

On the code below I have the prompt asking the user to put in w, a, s or d So i wanted these 4 keys to be returned right away on the click and if they put any other key have the terminal ignore it and not show it at all how do I do that?

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();
Mohamed
  • 425
  • 4
  • 14

1 Answers1

0

You can do something like this:


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();

It will call the userInput again if user presses any key other than allowed.

This is from the Node.js readline documentation:

The 'line' event is emitted whenever the input stream receives an end-of-line input (\n, \r, or \r\n). This usually occurs when the user presses Enter or Return.

So, this is how it works. It will invoke the call when it receives the end-of-line input else keep waiting for the input. Check here.

Instead use the keypress to get the input from user like this:

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();

check this for more details.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • I had done that with switch statement but I want the function to exit on press basically when they press 'a' i want it right away to be executed and they dont have to basically press the enter key and then when they press any other letters I dont even want them to be printed or show up at all so they being removed completely. Hope this make sense – Mohamed Apr 29 '21 at 20:16
  • You should check out the working of readline as It is important to understand the working of any module first and then expect o/p from it. If you have done something that works with switch statement its great. – Apoorva Chikara Apr 30 '21 at 05:21
  • Does this help or you still have issue? – Apoorva Chikara Apr 30 '21 at 12:52