Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Last active May 22, 2023 17:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lahmatiy/9b44078e1ecdf81c4718 to your computer and use it in GitHub Desktop.
Save lahmatiy/9b44078e1ecdf81c4718 to your computer and use it in GitHub Desktop.
Snippet for node.js to prevent any user input while script is working, but process ctrl+c to exit
// doesn't work w/o this interface creation
require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// stop process input
process.stdin.pause();
// read from stdin 10 times per second to check if ctrl+c pressed
setInterval(function(){
var chunk = process.stdin.read();
if (chunk)
for (var i = 0; i < chunk.length; i++) {
if (chunk[i] == 3) // ^C
process.exit();
}
}, 100);
@Kenhuey
Copy link

Kenhuey commented Oct 17, 2021

Thanks, useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment