Reputation: 100020
Traditionally, command/ctrl + (lowercase L) in a terminal will clear it.
I can clear the terminal using Node.js by writing this to stdout:
process.stdout.write('\x1Bc');
but how can I listen for signals specifically a "command + l" key combination? (that is a lowercase L).
I think with Node.js the answer is probably related to:
readline.emitKeypressEvents
https://nodejs.org/api/readline.html#readline_readline_emitkeypressevents_stream_interface
Upvotes: 0
Views: 1136
Reputation: 557
I dig up a bit and wrote a quick script as a proof of concept. That is an example of handling Ctrl-C, Ctrl-D and Ctrl-L signals AND having an event listener on new lines, even with raw mode enabled.
#!/usr/bin/env node
process.stdin.currentLine = '';
process.stdin.setRawMode(true);
process.stdin.on('data', (buf) => {
const charAsAscii = buf.toString().charCodeAt(0);
switch (charAsAscii) {
case 0x03:
console.log('You pressed Ctrl-C. Sending SIGINT.')
process.kill(process.pid, 'SIGINT');
break;
case 0x04:
console.log('You pressed Ctrl-D. Bye!')
process.exit(0);
break;
case 0x0c:
console.log('You pressed Ctrl-L. Clearing screen.')
break;
case 0x0d:
process.stdin.emit('line', process.stdin.currentLine);
process.stdin.currentLine = '';
break;
default:
process.stdin.currentLine += String.fromCharCode(charAsAscii);
break;
}
});
process.stdin.on('line', line => console.log(`New line: ${line}`));
How does it work:
When in raw mode, input is always available character-by-character, not including modifiers. Additionally, all special processing of characters by the terminal is disabled, including echoing input characters. Note that CTRL+C will no longer cause a SIGINT when in this mode.
With that in mind, you can convert the buffer you get in the 'data' event, and check if it's a SIGINT (Ctrl-C), a 'clear' command (Ctrl-L), an EOF (Ctrl-D), ...
If it's a normal character, it simply adds the character to an internal buffer. When the return key is pressed, a line
event is emitted with the internal buffer as argument. Then, the internal buffer is reset to an empty string.
Upvotes: 1