From a3f120a131223bf8d3af9597844b09d4ca9c5da8 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Wed, 25 Sep 2019 21:23:04 +0000
Subject: [PATCH] Turn the keybuf into an array rather than a string so that
 extended keys can go into it. Add support for (some) extended keys with the
 local console.

---
 exec/dorkit/local_console.js | 35 ++++++++++++++++++++++++++++
 exec/load/dorkit.js          | 44 ++++++++++++++++++++----------------
 2 files changed, 60 insertions(+), 19 deletions(-)

diff --git a/exec/dorkit/local_console.js b/exec/dorkit/local_console.js
index 5382917a78..b489d6123b 100644
--- a/exec/dorkit/local_console.js
+++ b/exec/dorkit/local_console.js
@@ -9,7 +9,42 @@ if (js.global.conio !== undefined && dk.console.local) {
 	conio.setcursortype(2);
 	dk.console.input_queue_callback.push(function() {
 		'use strict';
+
+		var ch;
 		if (conio.kbhit) {
+			ch = conio.getch();
+
+			if (ch === 0) {
+				ch = conio.getch();
+				switch(ch) {
+					case 0x47:
+						return dk.console.key.KEY_HOME;
+					case 72:
+						return dk.console.key.KEY_UP;
+					case 0x4f:
+						return dk.console.key.KEY_END;
+					case 80:
+						return dk.console.key.KEY_DOWN;
+					case 0x52:
+						return dk.console.key.KEY_INS;
+					case 0x53:
+						return dk.console.key.KEY_DEL;
+					case 0x4b:
+						return dk.console.key.KEY_LEFT;
+					case 0x4d:
+						return dk.console.key.KEY_RIGHT;
+					case 0x49:
+						return dk.console.key.KEY_PGUP;
+					case 0x51:
+						return dk.console.key.KEY_PGDOWN;
+					default:
+						if (ch >= 0x3a && ch <= 0x44)
+							return dk.console.key['KEY_F'+(ch - 0x39)];
+						if (ch >= 0x7a && ch <= 0x7b)
+							return dk.console.key['KEY_F'+(ch - 0x6f)];
+				}
+				return;
+			}
 			return ascii(conio.getch());
 		}
 	});
diff --git a/exec/load/dorkit.js b/exec/load/dorkit.js
index c9a3270098..db1ba0b25d 100644
--- a/exec/load/dorkit.js
+++ b/exec/load/dorkit.js
@@ -257,7 +257,7 @@ var dk = {
 		rows:24,				// Rows in users terminal
 		cols:80,				// Columns in users terminal
 
-		keybuf:'',
+		keybuf:[],
 		input_queue:new Queue("dorkit_input" + (js.global.bbs === undefined ? '' : bbs.node_num)),
 
 		/*
@@ -392,7 +392,7 @@ var dk = {
 		 */
 		getblock:function(sx,sy,ex,ey) {
 			'use strict';
-			return this.remote_screen.graphic.get(sx,sy,ex,ey);
+			return this.local_io.screen.graphic.get(sx,sy,ex,ey);
 		},
 
 		/*
@@ -480,7 +480,7 @@ var dk = {
 					for (i = 0; i < this.input_queue_callback.length; i += 1) {
 						d = this.input_queue_callback[i]();
 						if (d !== undefined) {
-							this.keybuf += d;
+							this.keybuf.push(d);
 						}
 					}
 					if (this.keybuf.length > 0) {
@@ -505,8 +505,7 @@ var dk = {
 			var m;
 
 			if (this.keybuf.length > 0) {
-				ret = this.keybuf[0];
-				this.keybuf = this.keybuf.substr(1);
+				ret = this.keybuf.shift();
 				return ret;
 			}
 			if (!this.waitkey(0)) {
@@ -553,21 +552,28 @@ var dk = {
 			'use strict';
 			var ret;
 
-			if (this.keybuf.length > 0) {
-				ret = this.keybuf[0];
-				this.keybuf = this.keybuf.substr(1);
-				return ret;
-			}
-			if (!this.waitkey(0)) {
-				return undefined;
-			}
-			ret = this.input_queue.read();
-			if (ret.length > 1) {
-				ret=ret.replace(/^.*\x00/,'');
-				this.keybuf = ret.substr(1);
-				ret = ret[0];
+			while (1) {
+				if (this.keybuf.length > 0) {
+					do {
+						ret = this.keybuf.shift();
+					} while(ret.length > 1 && ret.indexOf('\x00') === -1);
+					return ret;
+				}
+				if (!this.waitkey(0)) {
+					return undefined;
+				}
+				ret = this.input_queue.read();
+				if (ret.length > 1) {
+					if (ret.indexOf('\x00') > -1) {
+						ret=ret.replace(/^.*\x00/,'');
+						ret.split('').forEach(function(ch) {
+							this.keybuf.push(ch);
+						}, this);
+					}
+				}
+				else
+					return ret;
 			}
-			return ret;
 		},
 		getstr_defaults:{
 			timeout:undefined,	// Timeout, undefined for "wait forever"
-- 
GitLab