Handling CTRL+C event in Node.js on Windows

夙愿已清 提交于 2019-12-04 03:06:13

I used this piece of code for listening for keys. It seems to work for CTRL + C as well on Windows.

But then again it only works for CTRL + C as a key combination, not anything else. Of course, you could both bind a function to process.on("exit", and call it inside the if block below.

var tty = require("tty");

process.openStdin().on("keypress", function(chunk, key) {
  if(key && key.name === "c" && key.ctrl) {
    console.log("bye bye");
    process.exit();
  }
});

tty.setRawMode(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!