index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * This module offers the internal "keypress" functionality from node-core's
  3. * `readline` module, for your own programs and modules to use.
  4. *
  5. * Usage:
  6. *
  7. * require('keypress')(process.stdin);
  8. *
  9. * process.stdin.on('keypress', function (ch, key) {
  10. * console.log(ch, key);
  11. * if (key.ctrl && key.name == 'c') {
  12. * process.stdin.pause();
  13. * }
  14. * });
  15. * proces.stdin.resume();
  16. */
  17. var exports = module.exports = keypress;
  18. exports.enableMouse = function (stream) {
  19. stream.write('\x1b' +'[?1000h')
  20. }
  21. exports.disableMouse = function (stream) {
  22. stream.write('\x1b' +'[?1000l')
  23. }
  24. /**
  25. * accepts a readable Stream instance and makes it emit "keypress" events
  26. */
  27. function keypress(stream) {
  28. if (isEmittingKeypress(stream)) return;
  29. stream._emitKeypress = true;
  30. function onData(b) {
  31. if (stream.listeners('keypress').length > 0) {
  32. emitKey(stream, b);
  33. } else {
  34. // Nobody's watching anyway
  35. stream.removeListener('data', onData);
  36. stream.on('newListener', onNewListener);
  37. }
  38. }
  39. function onNewListener(event) {
  40. if (event == 'keypress') {
  41. stream.on('data', onData);
  42. stream.removeListener('newListener', onNewListener);
  43. }
  44. }
  45. if (stream.listeners('keypress').length > 0) {
  46. stream.on('data', onData);
  47. } else {
  48. stream.on('newListener', onNewListener);
  49. }
  50. }
  51. /**
  52. * Returns `true` if the stream is already emitting "keypress" events.
  53. * `false` otherwise.
  54. */
  55. function isEmittingKeypress(stream) {
  56. var rtn = stream._emitKeypress;
  57. if (!rtn) {
  58. // hack: check for the v0.6.x "data" event
  59. stream.listeners('data').forEach(function (l) {
  60. if (l.name == 'onData' && /emitKey/.test(l.toString())) {
  61. rtn = true;
  62. stream._emitKeypress = true;
  63. }
  64. });
  65. }
  66. if (!rtn) {
  67. // hack: check for the v0.6.x "newListener" event
  68. stream.listeners('newListener').forEach(function (l) {
  69. if (l.name == 'onNewListener' && /keypress/.test(l.toString())) {
  70. rtn = true;
  71. stream._emitKeypress = true;
  72. }
  73. });
  74. }
  75. return rtn;
  76. }
  77. /*
  78. Some patterns seen in terminal key escape codes, derived from combos seen
  79. at http://www.midnight-commander.org/browser/lib/tty/key.c
  80. ESC letter
  81. ESC [ letter
  82. ESC [ modifier letter
  83. ESC [ 1 ; modifier letter
  84. ESC [ num char
  85. ESC [ num ; modifier char
  86. ESC O letter
  87. ESC O modifier letter
  88. ESC O 1 ; modifier letter
  89. ESC N letter
  90. ESC [ [ num ; modifier char
  91. ESC [ [ 1 ; modifier letter
  92. ESC ESC [ num char
  93. ESC ESC O letter
  94. - char is usually ~ but $ and ^ also happen with rxvt
  95. - modifier is 1 +
  96. (shift * 1) +
  97. (left_alt * 2) +
  98. (ctrl * 4) +
  99. (right_alt * 8)
  100. - two leading ESCs apparently mean the same as one leading ESC
  101. */
  102. // Regexes used for ansi escape code splitting
  103. var metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/;
  104. var functionKeyCodeRe =
  105. /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/;
  106. function emitKey(stream, s) {
  107. var ch,
  108. key = {
  109. name: undefined,
  110. ctrl: false,
  111. meta: false,
  112. shift: false
  113. },
  114. parts;
  115. if (Buffer.isBuffer(s)) {
  116. if (s[0] > 127 && s[1] === undefined) {
  117. s[0] -= 128;
  118. s = '\x1b' + s.toString(stream.encoding || 'utf-8');
  119. } else {
  120. s = s.toString(stream.encoding || 'utf-8');
  121. }
  122. }
  123. key.sequence = s;
  124. if (s === '\r' || s === '\n') {
  125. // enter
  126. key.name = 'enter';
  127. } else if (s === '\t') {
  128. // tab
  129. key.name = 'tab';
  130. } else if (s === '\b' || s === '\x7f' ||
  131. s === '\x1b\x7f' || s === '\x1b\b') {
  132. // backspace or ctrl+h
  133. key.name = 'backspace';
  134. key.meta = (s.charAt(0) === '\x1b');
  135. } else if (s === '\x1b' || s === '\x1b\x1b') {
  136. // escape key
  137. key.name = 'escape';
  138. key.meta = (s.length === 2);
  139. } else if (s === ' ' || s === '\x1b ') {
  140. key.name = 'space';
  141. key.meta = (s.length === 2);
  142. } else if (s <= '\x1a') {
  143. // ctrl+letter
  144. key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
  145. key.ctrl = true;
  146. } else if (s.length === 1 && s >= 'a' && s <= 'z') {
  147. // lowercase letter
  148. key.name = s;
  149. } else if (s.length === 1 && s >= 'A' && s <= 'Z') {
  150. // shift+letter
  151. key.name = s.toLowerCase();
  152. key.shift = true;
  153. } else if (parts = metaKeyCodeRe.exec(s)) {
  154. // meta+character key
  155. key.name = parts[1].toLowerCase();
  156. key.meta = true;
  157. key.shift = /^[A-Z]$/.test(parts[1]);
  158. } else if (parts = functionKeyCodeRe.exec(s)) {
  159. // ansi escape sequence
  160. // reassemble the key code leaving out leading \x1b's,
  161. // the modifier key bitflag and any meaningless "1;" sequence
  162. var code = (parts[1] || '') + (parts[2] || '') +
  163. (parts[4] || '') + (parts[6] || ''),
  164. modifier = (parts[3] || parts[5] || 1) - 1;
  165. // Parse the key modifier
  166. key.ctrl = !!(modifier & 4);
  167. key.meta = !!(modifier & 10);
  168. key.shift = !!(modifier & 1);
  169. key.code = code;
  170. // Parse the key itself
  171. switch (code) {
  172. /* xterm/gnome ESC O letter */
  173. case 'OP': key.name = 'f1'; break;
  174. case 'OQ': key.name = 'f2'; break;
  175. case 'OR': key.name = 'f3'; break;
  176. case 'OS': key.name = 'f4'; break;
  177. /* xterm/rxvt ESC [ number ~ */
  178. case '[11~': key.name = 'f1'; break;
  179. case '[12~': key.name = 'f2'; break;
  180. case '[13~': key.name = 'f3'; break;
  181. case '[14~': key.name = 'f4'; break;
  182. /* from Cygwin and used in libuv */
  183. case '[[A': key.name = 'f1'; break;
  184. case '[[B': key.name = 'f2'; break;
  185. case '[[C': key.name = 'f3'; break;
  186. case '[[D': key.name = 'f4'; break;
  187. case '[[E': key.name = 'f5'; break;
  188. /* common */
  189. case '[15~': key.name = 'f5'; break;
  190. case '[17~': key.name = 'f6'; break;
  191. case '[18~': key.name = 'f7'; break;
  192. case '[19~': key.name = 'f8'; break;
  193. case '[20~': key.name = 'f9'; break;
  194. case '[21~': key.name = 'f10'; break;
  195. case '[23~': key.name = 'f11'; break;
  196. case '[24~': key.name = 'f12'; break;
  197. /* xterm ESC [ letter */
  198. case '[A': key.name = 'up'; break;
  199. case '[B': key.name = 'down'; break;
  200. case '[C': key.name = 'right'; break;
  201. case '[D': key.name = 'left'; break;
  202. case '[E': key.name = 'clear'; break;
  203. case '[F': key.name = 'end'; break;
  204. case '[H': key.name = 'home'; break;
  205. /* xterm/gnome ESC O letter */
  206. case 'OA': key.name = 'up'; break;
  207. case 'OB': key.name = 'down'; break;
  208. case 'OC': key.name = 'right'; break;
  209. case 'OD': key.name = 'left'; break;
  210. case 'OE': key.name = 'clear'; break;
  211. case 'OF': key.name = 'end'; break;
  212. case 'OH': key.name = 'home'; break;
  213. /* xterm/rxvt ESC [ number ~ */
  214. case '[1~': key.name = 'home'; break;
  215. case '[2~': key.name = 'insert'; break;
  216. case '[3~': key.name = 'delete'; break;
  217. case '[4~': key.name = 'end'; break;
  218. case '[5~': key.name = 'pageup'; break;
  219. case '[6~': key.name = 'pagedown'; break;
  220. /* putty */
  221. case '[[5~': key.name = 'pageup'; break;
  222. case '[[6~': key.name = 'pagedown'; break;
  223. /* rxvt */
  224. case '[7~': key.name = 'home'; break;
  225. case '[8~': key.name = 'end'; break;
  226. /* rxvt keys with modifiers */
  227. case '[a': key.name = 'up'; key.shift = true; break;
  228. case '[b': key.name = 'down'; key.shift = true; break;
  229. case '[c': key.name = 'right'; key.shift = true; break;
  230. case '[d': key.name = 'left'; key.shift = true; break;
  231. case '[e': key.name = 'clear'; key.shift = true; break;
  232. case '[2$': key.name = 'insert'; key.shift = true; break;
  233. case '[3$': key.name = 'delete'; key.shift = true; break;
  234. case '[5$': key.name = 'pageup'; key.shift = true; break;
  235. case '[6$': key.name = 'pagedown'; key.shift = true; break;
  236. case '[7$': key.name = 'home'; key.shift = true; break;
  237. case '[8$': key.name = 'end'; key.shift = true; break;
  238. case 'Oa': key.name = 'up'; key.ctrl = true; break;
  239. case 'Ob': key.name = 'down'; key.ctrl = true; break;
  240. case 'Oc': key.name = 'right'; key.ctrl = true; break;
  241. case 'Od': key.name = 'left'; key.ctrl = true; break;
  242. case 'Oe': key.name = 'clear'; key.ctrl = true; break;
  243. case '[2^': key.name = 'insert'; key.ctrl = true; break;
  244. case '[3^': key.name = 'delete'; key.ctrl = true; break;
  245. case '[5^': key.name = 'pageup'; key.ctrl = true; break;
  246. case '[6^': key.name = 'pagedown'; key.ctrl = true; break;
  247. case '[7^': key.name = 'home'; key.ctrl = true; break;
  248. case '[8^': key.name = 'end'; key.ctrl = true; break;
  249. /* misc. */
  250. case '[Z': key.name = 'tab'; key.shift = true; break;
  251. default: key.name = 'undefined'; break;
  252. }
  253. } else if (s.length > 1 && s[0] !== '\x1b') {
  254. // Got a longer-than-one string of characters.
  255. // Probably a paste, since it wasn't a control sequence.
  256. Array.prototype.forEach.call(s, function(c) {
  257. emitKey(stream, c);
  258. });
  259. return;
  260. }
  261. if (key.code == '[M') {
  262. key.name = 'mouse';
  263. var s = key.sequence;
  264. var b = s.charCodeAt(3);
  265. key.x = s.charCodeAt(4) - 040;
  266. key.y = s.charCodeAt(5) - 040;
  267. key.scroll = 0;
  268. key.ctrl = !!(1<<4 & b);
  269. key.meta = !!(1<<3 & b);
  270. key.shift = !!(1<<2 & b);
  271. key.release = (3 & b) === 3;
  272. if (1<<6 & b) { //scroll
  273. key.scroll = 1 & b ? 1 : -1;
  274. }
  275. if (!key.release && !key.scroll) {
  276. key.button = b & 3;
  277. }
  278. }
  279. // Don't emit a key if no name was found
  280. if (key.name === undefined) {
  281. key = undefined;
  282. }
  283. if (s.length === 1) {
  284. ch = s;
  285. }
  286. if (key && key.name == 'mouse') {
  287. stream.emit('mousepress', key)
  288. } else if (key || ch) {
  289. stream.emit('keypress', ch, key);
  290. }
  291. }