如何使用节点和 JavaScript 在按 Enter 之前从终端读取用户输入?

发布于 2025-01-15 14:50:55 字数 361 浏览 3 评论 0原文

如何使用节点和 JavaScript 在按 Enter 之前从终端读取用户输入?

我制作了一个简单的 javascript 应用程序,它使用 process.stdin 或 readline 来获取用户输入,但我不希望用户必须使用 Enter/Return 提交输入。我想读取按键/按键上的用户输入。这可能吗?我怎样才能做到这一点?谢谢!

要求:

  • javascript、节点
  • 来自终端用户的
  • 不需要通过 Enter/Return 提交字符串

首选:

  • 更少的库,更多的普通 javascript
  • 处理任何键:字母、数字、箭头键、修饰符

How to read user input from terminal before pressing enter using node and javascript?

I have made a simple javascript application which uses process.stdin or readline to get user input, but I don't want the user to have to submit their input with enter/return. I'd like to read user input on keydown/keypress. Is this possible? How might I accomplish this? Thanks!

Requirements:

  • javascript, node
  • from terminal
  • user not required to submit string with enter/return

Prefer:

  • less libraries, more vanilla javascript
  • handles any key: letters, numbers, arrow keys, modifiers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2025-01-22 14:50:55

一种简单的方法是使用 iohook

Node.JS 的原生方式是这样的。

require("readline").emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

process.stdin.on("keypress", (char, evt) => {
  console.log("=====Key pressed=====");
  console.log("Char:", JSON.stringify(char), "Evt:", JSON.stringify(evt));

  if (char === "h") console.log("Hello World!");
  if (char === "q") process.exit();
});

第一行 require("readline").emitKeypressEvents(process.stdin) 使 process.stdin 发出按键事件,因为它通常不发出该事件。

第二个 process.stdin.setRawMode(true) 使 process.stdin 成为原始设备。在原始设备配置的流中,按键事件是按字符发出的,而不是按回车键发出的。

然后,将 keypress 事件侦听器添加到 process.stdin 中以处理按键。

注意

process.stdin 转换为原始设备时,Ctrl+C 不会发出 SIGINT 信号,换句话说,Ctrl+C 不会停止程序。这意味着您需要手动绑定一个键才能退出。

An easy way to do it could be with the iohook package.

The native Node.JS way to do it would be like this.

require("readline").emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

process.stdin.on("keypress", (char, evt) => {
  console.log("=====Key pressed=====");
  console.log("Char:", JSON.stringify(char), "Evt:", JSON.stringify(evt));

  if (char === "h") console.log("Hello World!");
  if (char === "q") process.exit();
});

The first line, require("readline").emitKeypressEvents(process.stdin) makes process.stdin emit keypress events, as it normally does not emit the event.

The second, process.stdin.setRawMode(true) makes process.stdin a raw device. In a raw device configured stream, key press events are emitted on a per character basis, instead of emitting per enter key press.

Then, the keypress event listener is added onto process.stdin to handle keypresses.

Note

When process.stdin is converted to a raw device, Ctrl+C does not emit a SIGINT signal, in other words, Ctrl+C will not stop the program. This means that you will need to manually bind a key to exit.

佼人 2025-01-22 14:50:55

这就是我在节点 16.20.1 上的工作:

process.stdin.setRawMode(true);
process.stdin.on("data", function(charBuffer) {
  const char = charBuffer.toString();
  if (char === "q") {
    process.exit();
  }
  console.log('do anything you want with this:', char);
});

This was what worked for me on node 16.20.1:

process.stdin.setRawMode(true);
process.stdin.on("data", function(charBuffer) {
  const char = charBuffer.toString();
  if (char === "q") {
    process.exit();
  }
  console.log('do anything you want with this:', char);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文