Skip to content

Instantly share code, notes, and snippets.

@sefgit
Forked from unitycoder/node-presskey-console.js
Created July 20, 2024 03:24
Show Gist options
  • Save sefgit/7a4c8d66955dcc6ecb40336453bf27da to your computer and use it in GitHub Desktop.
Save sefgit/7a4c8d66955dcc6ecb40336453bf27da to your computer and use it in GitHub Desktop.
node.js Wait For KeyPress in console
// node.js get keypress
var stdin = process.stdin;
// without this, we would only get streams once enter is pressed
//stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();
// i don't want binary, do you?
stdin.setEncoding( 'utf8' );
// on any data into stdin
stdin.on( 'data', function( key )
{
// ctrl-c ( end of text )
if ( key === '\u0003' ) {
process.exit();
}
// without rawmode, it returns EOL with the string
if (key.indexOf('1')==0)
{
console.log("press 1");
}
if (key.indexOf('2')==0)
{
console.log("press 2");
}
// write the key to stdout all normal like
// process.stdout.write( key );
});
@sefgit
Copy link
Author

sefgit commented Jul 20, 2024

if(key.ctrl && ...)

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