在节点:readline中按CTRL+ d时会发出什么信号?
我一直在尝试拦截 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??')
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
控制 + d 不是像sigint这样的常规操作系统信号。
它在stdin上用作readline和许多其他人关闭STDIN的信息。这是 1 处理它:
我看不到一种在不超过读取条件的情况下在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:
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