在节点:readline中按CTRL+ d时会发出什么信号?

发布于 2025-02-13 12:51:24 字数 809 浏览 0 评论 0 原文

我一直在尝试拦截 ctrl+d ,以便我可以用它代表我正在构建的CLI代表“文档”。我一直没有成功地识别如何使用按键使用读取线时拦截它。

在按下CTRL+D时,我是否可以收听并覆盖默认的 readline 行为以退出应用程序的特定信号事件?

还值得注意的是,如果按下其他键, ctrl+d 不会退出应用程序。但这是解决方案。

任何帮助将不胜感激!

import readline from'node:readline'

const stdin = process.stdin
stdin.setEncoding('utf8')
const stdout = process.stdout
stdout.setEncoding('utf8')

const rl = readline.createInterface( stdin, stdout )

readline.emitKeypressEvents(stdin)
stdin.setRawMode(true)

rl.on('line', line => {
  console.log(line)
})

stdin.on('keypress', async (str, key) => {  
  if(key.name === 'd' && key.ctrl === true){
    console.log('CTRL-D')
  }
})

process.on('SIGINT', ()=>{
  console.log('CTRL+D??')
})

I've been trying to intercept CTRL+D so I can use it to represent 'document' for a CLI I am building. I have been unsuccessful identifying how to intercept it when using readline with keypress.

Is there a specific signal event I can listen for and override the default readline behavior to exit the application when CTRL+D is pressed?

It's also notable that CTRL+D does not exit the application if other keys have been pressed. But this is untenable as a solution.

Any help would be appreciated!

import readline from'node:readline'

const stdin = process.stdin
stdin.setEncoding('utf8')
const stdout = process.stdout
stdout.setEncoding('utf8')

const rl = readline.createInterface( stdin, stdout )

readline.emitKeypressEvents(stdin)
stdin.setRawMode(true)

rl.on('line', line => {
  console.log(line)
})

stdin.on('keypress', async (str, key) => {  
  if(key.name === 'd' && key.ctrl === true){
    console.log('CTRL-D')
  }
})

process.on('SIGINT', ()=>{
  console.log('CTRL+D??')
})

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

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

发布评论

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

评论(1

漫漫岁月 2025-02-20 12:51:24

控制 + d 不是像sigint这样的常规操作系统信号。

它在stdin上用作readline和许多其他人关闭STDIN的信息。这是 1 处理它:

        case 'd': // delete right or EOF
          if (this.cursor === 0 && this.line.length === 0) {
            // This readline instance is finished
            this.close();
          } else if (this.cursor < this.line.length) {
            this[kDeleteRight]();
          }
          break;

我看不到一种在不超过读取条件的情况下在readline中更改此行为的方法斯丁。

1-麻省理工学院许可。版权节点贡献者

Control + D is not a regular operating system signal like SIGINT.

It is used on stdin as "end-of-transmission" information by readline and many others to close stdin. Here is a snippet of the node readline source1 handling it:

        case 'd': // delete right or EOF
          if (this.cursor === 0 && this.line.length === 0) {
            // This readline instance is finished
            this.close();
          } else if (this.cursor < this.line.length) {
            this[kDeleteRight]();
          }
          break;

I can't see a way of changing this behaviour in readline without overiding readlines _ttyWrite function or some type of interception before readline handles stdin.

1 - MIT Licensed. Copyright Node.js contributors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文