public
Created

Enable "mouse reporting" with Node.js

  • Download Gist
mouse.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// 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');
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.