Created
January 30, 2012 05:55
-
-
Save TooTallNate/1702813 to your computer and use it in GitHub Desktop.
Enable "mouse reporting" with Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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: