Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

press any key to continue in nodejs

I need a function that will pause the execution of the script until a key is pressed. I've tried:

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

but it's just listening for a keypress and nothing happens. The program does not continue executing.

How can I pause execution?

like image 799
Barterio Avatar asked Oct 30 '13 15:10

Barterio


People also ask

How do you repeat node JS?

You use repeating to print repeating strings: const repeating = require('repeating'); console. log(repeating(100, 'unicorn '));

What is next () node?

The next() function is not a part of the Node. js or Express API, but is the third argument that is passed to the middleware function. The next() function could be named anything, but by convention it is always named “next”. To avoid confusion, always use this convention. To load the middleware function, call app.

How do you stop an infinite loop in node JS?

Use window. stop() to prevent the page from loading and running. For NodeJS only – Use process. abort() or process.

What is key in node JS?

keys() The NodeList. keys() method returns an iterator allowing to go through all keys contained in this object. The keys are unsigned integer .


5 Answers

In node.js 7.6 and later you can do this:

const keypress = async () => {
  process.stdin.setRawMode(true)
  return new Promise(resolve => process.stdin.once('data', () => {
    process.stdin.setRawMode(false)
    resolve()
  }))
}

;(async () => {

  console.log('program started, press any key to continue')
  await keypress()
  console.log('program still running, press any key to continue')
  await keypress()
  console.log('bye')

})().then(process.exit)

Or if you want CTRL-C to exit the program but any other key to continue normal execution, then you can replace the "keypress" function above with this function instead:

const keypress = async () => {
  process.stdin.setRawMode(true)
  return new Promise(resolve => process.stdin.once('data', data => {
    const byteArray = [...data]
    if (byteArray.length > 0 && byteArray[0] === 3) {
      console.log('^C')
      process.exit(1)
    }
    process.stdin.setRawMode(false)
    resolve()
  }))
}
like image 80
molsson Avatar answered Oct 19 '22 20:10

molsson


Works for me:

console.log('Press any key to exit');

process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
like image 29
vkurchatkin Avatar answered Oct 19 '22 22:10

vkurchatkin


The accepted solution waits asynchronously for a key event and then exits, it is not really a solution to "Press any key to continue".

I needed to pause while writing some nodejs shell scripts. I ended up using the spawnSync of the child_process with the shell command "read".

This will basically pause the script and when you press Enter it will continue. Much like the pause command in windows.

require('child_process').spawnSync("read _ ", {shell: true, stdio: [0, 1, 2]});

Hope this helps.

like image 33
Ralph Avatar answered Oct 19 '22 21:10

Ralph


This snippet does the job if you don't want to exit the process:

console.log('Press any key to continue.');
process.stdin.once('data', function () {
  continueDoingStuff();
});

It's async so won't work inside loop as-is-- if you're using Node 7 you could wrap it in a promise and use async/await.

like image 11
NeonPaul Avatar answered Oct 19 '22 21:10

NeonPaul


There is a package for this: press-any-key

And here is an example:

const pressAnyKey = require('press-any-key');
pressAnyKey("Press any key to resolve, or CTRL+C to reject", {
  ctrlC: "reject"
})
  .then(() => {
    console.log('You pressed any key')
  })
  .catch(() => {
    console.log('You pressed CTRL+C')
  })

It runs without problems in W10.

like image 5
Josem Avatar answered Oct 19 '22 21:10

Josem