按任意键在 nodejs 中继续

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19687407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:07:59  来源:igfitidea点击:

press any key to continue in nodejs

javascriptnode.jskeypress

提问by Barterio

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?

如何暂停执行?

采纳答案by vkurchatkin

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

回答by molsson

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

在 node.js 7.6 及更高版本中,您可以执行以下操作:

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:

或者,如果您希望 CTRL-C 退出程序但任何​​其他键继续正常执行,那么您可以用此函数替换上面的“keypress”函数:

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

回答by NeonPaul

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.

它是异步的,因此不能按原样在循环内工作——如果您使用的是 Node 7,您可以将它包装在一个 promise 中并使用async/await.

回答by Ralph

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".

我在编写一些 nodejs shell 脚本时需要暂停。我最终将 child_process 的 spawnSync 与 shell 命令“read”一起使用。

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

这将基本上暂停脚本,当您按 Enter 时,它将继续。很像 windows 中的 pause 命令。

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

Hope this helps.

希望这可以帮助。

回答by Wong Jia Hau

var fs = require("fs")
var fd = fs.openSync("/dev/stdin", "rs")
fs.readSync(fd, new Buffer(1), 0, 1)
fs.closeSync(fd)

This answer is taken from vadzim from node.js: readSync from stdin?

这个答案取自node.js 的 vadzim: readSync from stdin?

回答by Josem

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

有一个包: 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.

它在 W10 中运行没有问题。

回答by matpop

const fs = require('fs');
process.stdin.setRawMode(true);
fs.readSync(0, Buffer.alloc(1), 0, 1);

60% of the time, it works every time.

60% 的时间,它每次都有效。

回答by justin.m.chase

I actually made an npm package called paktcthat will help you with this. If you install the package:

我实际上制作了一个名为 npm 的包paktc,它可以帮助你解决这个问题。如果安装软件包:

> npm install paktc

Then you would use it like this:

然后你会像这样使用它:

// your console application code here...

require('paktc') // Press any key to continue...