-
-
Save TooTallNate/1702813 to your computer and use it in GitHub Desktop.
// Based on: | |
// http://groups.google.com/group/nodejs-dev/browse_thread/thread/a0c23008029e5fa7 | |
process.stdin.resume(); | |
process.stdin.on('data', function (b) { | |
var s = b.toString('utf8'); | |
if (s === '\u0003') { | |
console.error('Ctrl+C'); | |
process.stdin.pause(); | |
} else if (/^\u001b\[M/.test(s)) { | |
// mouse event | |
console.error('s.length:', s.length); | |
// reuse the key array albeit its name | |
// otherwise recompute as the mouse event is structured differently | |
var modifier = s.charCodeAt(3); | |
var key = {}; | |
key.shift = !!(modifier & 4); | |
key.meta = !!(modifier & 8); | |
key.ctrl = !!(modifier & 16); | |
key.x = s.charCodeAt(4) - 32; | |
key.y = s.charCodeAt(5) - 32; | |
key.button = null; | |
key.sequence = s; | |
key.buf = Buffer(key.sequence); | |
if ((modifier & 96) === 96) { | |
key.name = 'scroll'; | |
key.button = modifier & 1 ? 'down' : 'up'; | |
} else { | |
key.name = modifier & 64 ? 'move' : 'click'; | |
switch (modifier & 3) { | |
case 0 : key.button = 'left'; break; | |
case 1 : key.button = 'middle'; break; | |
case 2 : key.button = 'right'; break; | |
case 3 : key.button = 'none'; break; | |
default : return; | |
} | |
} | |
console.error(key); | |
} else { | |
// something else... | |
console.error(0, s, b); | |
} | |
}) | |
// Enable "raw mode" | |
if (process.stdin.setRawMode) { | |
process.stdin.setRawMode(true); | |
} else { | |
require('tty').setRawMode(true); | |
} | |
// Enable "mouse reporting" | |
process.stdout.write('\x1b[?1005h'); | |
process.stdout.write('\x1b[?1003h'); | |
process.on('exit', function () { | |
// don't forget to turn off mouse reporting | |
process.stdout.write('\x1b[?1005l'); | |
process.stdout.write('\x1b[?1003l'); | |
}); |
Hello! You have encountered the problem that after column 95 the buffer excludes the column counter until column 128 is reached? After that it works.
Since we all usually have a full HD or 4K screen this is obviously a problem.
@Elius94 almost a year late, but to you and any future internet wanderers that may stumble upon this gist on a search result like I did:
The reason it breaks between columns 95-128 (and rows, if you somehow had a display that tall) is because the mouse reporting mode used in that script is 1005
, which is archaic, deprecated and has the observed coordinates limitation, and it should not be used anymore (see the xterm control sequences documentation for more info on the mouse reporting modes)
The solution is to use mode 1006
instead (also documented in the above link), but it serializes the input feedback differently so simply changing the number isn't enough, the parsing code needs to be updated too, so here's a barebones fixed version:
// Tested on Node.js 20+, should also work on 18.x though
process.stdin.resume();
process.stdin.setRawMode(true);
process.stdout.write('\x1B[?1006h'); // Enable SGR mouse mode
process.stdout.write('\x1B[?1003h'); // Enable any event mouse mode
// 1000 -> only listen to button press and release
// 1002 -> listen to button press and release + mouse motion only while pressing button
// 1003 -> listen to button press and release + mouse motion at all times
process.on('exit', () => {
// disable all the modes
process.stdout.write('\x1B[?1006l');
process.stdout.write('\x1B[?1003l');
});
process.stdin.on('data', (buf) => {
const seq = buf.toString('utf8');
if (seq === '\u0003') {
console.error('Ctrl+C');
return process.stdin.pause();
}
if (!seq.startsWith('\x1B[<')) return; // not a mouse event
const [btn, x, y] = seq.slice(3, -1).split(';').map(Number);
const event = {};
event.button = btn & 0b11000011;
event.state = seq.at(-1) === 'M' ? 'pressed' : 'released';
event.x = x;
event.y = y;
event.motion = !!(btn & 0b00100000);
event.shift = !!(btn & 0b00000100);
event.meta = !!(btn & 0b00001000);
event.ctrl = !!(btn & 0b00010000);
console.log(event);
});
Thank you! This helped me a lot in a Project!