123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- var exports = module.exports = keypress;
- exports.enableMouse = function (stream) {
- stream.write('\x1b' +'[?1000h')
- }
- exports.disableMouse = function (stream) {
- stream.write('\x1b' +'[?1000l')
- }
- function keypress(stream) {
- if (isEmittingKeypress(stream)) return;
- stream._emitKeypress = true;
- function onData(b) {
- if (stream.listeners('keypress').length > 0) {
- emitKey(stream, b);
- } else {
-
- stream.removeListener('data', onData);
- stream.on('newListener', onNewListener);
- }
- }
- function onNewListener(event) {
- if (event == 'keypress') {
- stream.on('data', onData);
- stream.removeListener('newListener', onNewListener);
- }
- }
- if (stream.listeners('keypress').length > 0) {
- stream.on('data', onData);
- } else {
- stream.on('newListener', onNewListener);
- }
- }
- function isEmittingKeypress(stream) {
- var rtn = stream._emitKeypress;
- if (!rtn) {
-
- stream.listeners('data').forEach(function (l) {
- if (l.name == 'onData' && /emitKey/.test(l.toString())) {
- rtn = true;
- stream._emitKeypress = true;
- }
- });
- }
- if (!rtn) {
-
- stream.listeners('newListener').forEach(function (l) {
- if (l.name == 'onNewListener' && /keypress/.test(l.toString())) {
- rtn = true;
- stream._emitKeypress = true;
- }
- });
- }
- return rtn;
- }
- var metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/;
- var functionKeyCodeRe =
- /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/;
- function emitKey(stream, s) {
- var ch,
- key = {
- name: undefined,
- ctrl: false,
- meta: false,
- shift: false
- },
- parts;
- if (Buffer.isBuffer(s)) {
- if (s[0] > 127 && s[1] === undefined) {
- s[0] -= 128;
- s = '\x1b' + s.toString(stream.encoding || 'utf-8');
- } else {
- s = s.toString(stream.encoding || 'utf-8');
- }
- }
- key.sequence = s;
- if (s === '\r' || s === '\n') {
-
- key.name = 'enter';
- } else if (s === '\t') {
-
- key.name = 'tab';
- } else if (s === '\b' || s === '\x7f' ||
- s === '\x1b\x7f' || s === '\x1b\b') {
-
- key.name = 'backspace';
- key.meta = (s.charAt(0) === '\x1b');
- } else if (s === '\x1b' || s === '\x1b\x1b') {
-
- key.name = 'escape';
- key.meta = (s.length === 2);
- } else if (s === ' ' || s === '\x1b ') {
- key.name = 'space';
- key.meta = (s.length === 2);
- } else if (s <= '\x1a') {
-
- key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
- key.ctrl = true;
- } else if (s.length === 1 && s >= 'a' && s <= 'z') {
-
- key.name = s;
- } else if (s.length === 1 && s >= 'A' && s <= 'Z') {
-
- key.name = s.toLowerCase();
- key.shift = true;
- } else if (parts = metaKeyCodeRe.exec(s)) {
-
- key.name = parts[1].toLowerCase();
- key.meta = true;
- key.shift = /^[A-Z]$/.test(parts[1]);
- } else if (parts = functionKeyCodeRe.exec(s)) {
-
-
-
- var code = (parts[1] || '') + (parts[2] || '') +
- (parts[4] || '') + (parts[6] || ''),
- modifier = (parts[3] || parts[5] || 1) - 1;
-
- key.ctrl = !!(modifier & 4);
- key.meta = !!(modifier & 10);
- key.shift = !!(modifier & 1);
- key.code = code;
-
- switch (code) {
-
- case 'OP': key.name = 'f1'; break;
- case 'OQ': key.name = 'f2'; break;
- case 'OR': key.name = 'f3'; break;
- case 'OS': key.name = 'f4'; break;
-
- case '[11~': key.name = 'f1'; break;
- case '[12~': key.name = 'f2'; break;
- case '[13~': key.name = 'f3'; break;
- case '[14~': key.name = 'f4'; break;
-
- case '[[A': key.name = 'f1'; break;
- case '[[B': key.name = 'f2'; break;
- case '[[C': key.name = 'f3'; break;
- case '[[D': key.name = 'f4'; break;
- case '[[E': key.name = 'f5'; break;
-
- case '[15~': key.name = 'f5'; break;
- case '[17~': key.name = 'f6'; break;
- case '[18~': key.name = 'f7'; break;
- case '[19~': key.name = 'f8'; break;
- case '[20~': key.name = 'f9'; break;
- case '[21~': key.name = 'f10'; break;
- case '[23~': key.name = 'f11'; break;
- case '[24~': key.name = 'f12'; break;
-
- case '[A': key.name = 'up'; break;
- case '[B': key.name = 'down'; break;
- case '[C': key.name = 'right'; break;
- case '[D': key.name = 'left'; break;
- case '[E': key.name = 'clear'; break;
- case '[F': key.name = 'end'; break;
- case '[H': key.name = 'home'; break;
-
- case 'OA': key.name = 'up'; break;
- case 'OB': key.name = 'down'; break;
- case 'OC': key.name = 'right'; break;
- case 'OD': key.name = 'left'; break;
- case 'OE': key.name = 'clear'; break;
- case 'OF': key.name = 'end'; break;
- case 'OH': key.name = 'home'; break;
-
- case '[1~': key.name = 'home'; break;
- case '[2~': key.name = 'insert'; break;
- case '[3~': key.name = 'delete'; break;
- case '[4~': key.name = 'end'; break;
- case '[5~': key.name = 'pageup'; break;
- case '[6~': key.name = 'pagedown'; break;
-
- case '[[5~': key.name = 'pageup'; break;
- case '[[6~': key.name = 'pagedown'; break;
-
- case '[7~': key.name = 'home'; break;
- case '[8~': key.name = 'end'; break;
-
- case '[a': key.name = 'up'; key.shift = true; break;
- case '[b': key.name = 'down'; key.shift = true; break;
- case '[c': key.name = 'right'; key.shift = true; break;
- case '[d': key.name = 'left'; key.shift = true; break;
- case '[e': key.name = 'clear'; key.shift = true; break;
- case '[2$': key.name = 'insert'; key.shift = true; break;
- case '[3$': key.name = 'delete'; key.shift = true; break;
- case '[5$': key.name = 'pageup'; key.shift = true; break;
- case '[6$': key.name = 'pagedown'; key.shift = true; break;
- case '[7$': key.name = 'home'; key.shift = true; break;
- case '[8$': key.name = 'end'; key.shift = true; break;
- case 'Oa': key.name = 'up'; key.ctrl = true; break;
- case 'Ob': key.name = 'down'; key.ctrl = true; break;
- case 'Oc': key.name = 'right'; key.ctrl = true; break;
- case 'Od': key.name = 'left'; key.ctrl = true; break;
- case 'Oe': key.name = 'clear'; key.ctrl = true; break;
- case '[2^': key.name = 'insert'; key.ctrl = true; break;
- case '[3^': key.name = 'delete'; key.ctrl = true; break;
- case '[5^': key.name = 'pageup'; key.ctrl = true; break;
- case '[6^': key.name = 'pagedown'; key.ctrl = true; break;
- case '[7^': key.name = 'home'; key.ctrl = true; break;
- case '[8^': key.name = 'end'; key.ctrl = true; break;
-
- case '[Z': key.name = 'tab'; key.shift = true; break;
- default: key.name = 'undefined'; break;
- }
- } else if (s.length > 1 && s[0] !== '\x1b') {
-
-
- Array.prototype.forEach.call(s, function(c) {
- emitKey(stream, c);
- });
- return;
- }
- if (key.code == '[M') {
- key.name = 'mouse';
- var s = key.sequence;
- var b = s.charCodeAt(3);
- key.x = s.charCodeAt(4) - 040;
- key.y = s.charCodeAt(5) - 040;
- key.scroll = 0;
- key.ctrl = !!(1<<4 & b);
- key.meta = !!(1<<3 & b);
- key.shift = !!(1<<2 & b);
- key.release = (3 & b) === 3;
- if (1<<6 & b) {
- key.scroll = 1 & b ? 1 : -1;
- }
- if (!key.release && !key.scroll) {
- key.button = b & 3;
- }
- }
-
- if (key.name === undefined) {
- key = undefined;
- }
- if (s.length === 1) {
- ch = s;
- }
- if (key && key.name == 'mouse') {
- stream.emit('mousepress', key)
- } else if (key || ch) {
- stream.emit('keypress', ch, key);
- }
- }
|