reading keys using con.getch

This commit is contained in:
minjaesong
2021-04-07 21:14:04 +09:00
parent 907cb9c376
commit 922e24c6fd

View File

@@ -1,5 +1,6 @@
let scroll = 0; let scroll = 0;
let textbuffer = ["The quick brown fox","jumps over a lazy dog 12345678901234567890", "Pack my box with", "five dozen liquor jugs", "The quick brown fox","jumps over a lazy dog 12345678901234567890", "Pack my box with", "five dozen liquor jugs"]; //let textbuffer = ["The quick brown fox","jumps over a lazy dog 12345678901234567890", "Pack my box with", "five dozen liquor jugs", "The quick brown fox","jumps over a lazy dog 12345678901234567890", "Pack my box with", "five dozen liquor jugs"];
let textbuffer = [""];
let cursorRow = 0; let cursorRow = 0;
let cursorCol = 0; let cursorCol = 0;
let exit = false; let exit = false;
@@ -138,6 +139,7 @@ function drawTextbuffer() {
} }
function displayBulletin(text) { function displayBulletin(text) {
bulletinShown = true;
let txt = text.substring(0, windowWidth - 10); let txt = text.substring(0, windowWidth - 10);
con.move(windowHeight - 1, (windowWidth - txt.length) / 2); con.move(windowHeight - 1, (windowWidth - txt.length) / 2);
con.color_pair(COL_BACK, COL_TEXT); con.color_pair(COL_BACK, COL_TEXT);
@@ -146,6 +148,7 @@ function displayBulletin(text) {
gotoText(); gotoText();
} }
function dismissBulletin() { function dismissBulletin() {
bulletinShown = false;
drawTextLine(paintHeight - 1); drawTextLine(paintHeight - 1);
gotoText(); gotoText();
} }
@@ -155,42 +158,49 @@ function gotoText() {
} }
function hitCtrlS() { function hitCtrlS() {
sys.poke(-40, 1);
return (sys.peek(-41) == 47 && (sys.peek(-42) == 129 || sys.peek(-42) == 130)); return (sys.peek(-41) == 47 && (sys.peek(-42) == 129 || sys.peek(-42) == 130));
} }
function hitCtrlX() { function hitCtrlX() {
sys.poke(-40, 1);
return (sys.peek(-41) == 52 && (sys.peek(-42) == 129 || sys.peek(-42) == 130)); return (sys.peek(-41) == 52 && (sys.peek(-42) == 129 || sys.peek(-42) == 130));
} }
function hitAny() { function hitAny() {
sys.poke(-40, 1);
return sys.peek(-41) != 0; return sys.peek(-41) != 0;
} }
function appendText(code) {
if (textbuffer[cursorRow] === undefined)
textbuffer[cursorRow] = String.fromCharCode(code);
else
textbuffer[cursorRow] += String.fromCharCode(code);
cursorCol += 1;
drawTextLine(cursorRow - scroll);
gotoText();
}
reset_status(); reset_status();
drawMain(); drawMain();
drawTextbuffer(); drawTextbuffer();
let keyDown = false; let bulletinShown = false;
while (!exit) { while (!exit) {
// capture keys down let key = con.getch();
sys.poke(-40, 1);
if (!keyDown) { serial.println(key);
if (hitAny()) keyDown = true;
if (hitCtrlX()) { if (key == 24) // Ctrl-X
exit = true; exit = true;
} else if (key == 19 && !bulletinShown) {
else if (hitCtrlS()) { displayBulletin(`Wrote ${100 + ((Math.random() * 900)|0)} lines`);
displayBulletin("Wrote NaN lines"); }
} else if (key >= 32) { // printables (excludes \n)
else if (hitAny()) { if (bulletinShown) {
dismissBulletin(); dismissBulletin();
} }
}
sys.poke(-40, 1); appendText(key);
if (keyDown && !hitAny()) {
keyDown = false;
} }
serial.println("keydown = "+keyDown);
} }