Detecting CTRL+C in Node.js

前端 未结 2 1850
天命终不由人
天命终不由人 2020-12-08 01:38

I got this code from a different SO question, but node complained to use process.stdin.setRawMode instead of tty, so I changed it.

Before:

var tty =          


        
2条回答
  •  抹茶落季
    2020-12-08 01:57

    If you're trying to catch the interrupt signal SIGINT, you don't need to read from the keyboard. The process object of nodejs exposes an interrupt event:

    process.on('SIGINT', function() {
        console.log("Caught interrupt signal");
    
        if (i_should_exit)
            process.exit();
    });
    

    Edit: doesn't work on Windows without a workaround. See here

提交回复
热议问题