rewrote input.withEvent so that the typing is actually possible; edit now uses new input.withEvent()

This commit is contained in:
minjaesong
2021-10-04 18:21:46 +09:00
parent f049a22238
commit 7fe9e5c430
4 changed files with 70 additions and 28 deletions

View File

@@ -180,6 +180,7 @@ inputwork.stroboStatus = 0; // 0: first key, 1: waiting for initial delay, 2: re
inputwork.oldKeys = [];
inputwork.oldMouse = [];
inputwork.repeatCount = 0;
//inputwork.keyChanged = false;
/**
* callback: takes one argument of object which
@@ -206,28 +207,61 @@ input.withEvent = function(callback) {
}
function keysToStr(keys) {
let shiftin = keys.includes(59) || keys.includes(60);
//let shiftin = keys.includes(59) || keys.includes(60);
let headkey = keys.head();
return (inputwork.keymap[headkey] == undefined) ? undefined : (shiftin) ? (inputwork.keymap[headkey][1] || inputwork.keymap[headkey][0]) : inputwork.keymap[headkey][0];
//return (inputwork.keymap[headkey] == undefined) ? undefined : (shiftin) ? (inputwork.keymap[headkey][1] || inputwork.keymap[headkey][0]) : inputwork.keymap[headkey][0];
return (inputwork.keymap[headkey] == undefined) ? undefined : inputwork.keymap[headkey];
}
sys.poke(-40, 255);
let keys = [sys.peek(-41),sys.peek(-42),sys.peek(-43),sys.peek(-44),sys.peek(-45),sys.peek(-46),sys.peek(-47),sys.peek(-48)];
let mouse = [sys.peek(-33) | (sys.peek(-34) << 8), sys.peek(-35) | (sys.peek(-36) << 8), sys.peek(-37)];
let keyChanged = !arrayEq(keys, inputwork.oldKeys)
let keyDiff = arrayDiff(keys, inputwork.oldKeys)
// FIXME does not work with QMK's "LT(1,KC_ENT)" or something similar
//if (keys.sum() > 0) serial.println(`! captured=${keys.join()}`)
if (inputwork.stroboStatus % 2 == 0 && keys[0] != 0) {
inputwork.stroboStatus += 1;
inputwork.stroboTime = sys.nanoTime();
inputwork.repeatCount += 1;
callback(["key_down", keysToStr(keys), inputwork.repeatCount].concat(keys));
let shiftin = keys.includes(59) || keys.includes(60);
let keysym0 = keysToStr(keys);
let newKeysym0 = keysToStr(keyDiff);
let keysym = (keysym0 === undefined) ? undefined : (shiftin && keysym0[1]) ? keysym0[1] : keysym0[0];
let newKeysym = (newKeysym0 === undefined) ? undefined : (shiftin && newKeysym0[1]) ? newKeysym0[1] : newKeysym0[0];
//serial.println(`keys=${keys.join()}; oldkeys=${inputwork.oldKeys}; keyDiff=${keyDiff}`)
//serial.println(`keysym=${keysym}; newkeysym=${newKeysym}`)
if (!keyChanged) {
callback(["key_down", keysym, inputwork.repeatCount].concat(keys));
}
else if (newKeysym !== undefined) {
callback(["key_down", newKeysym, inputwork.repeatCount].concat(keys));
}
inputwork.oldKeys = keys; // don't put this outside of if-cascade
}
else if (!arrayEq(keys, inputwork.oldKeys) || keys[0] == 0) {
else if (keyChanged || keys[0] == 0) {
inputwork.stroboStatus = 0;
inputwork.repeatCount = 0;
if (inputwork.oldKeys[0] != 0)
callback(["key_change", keysToStr(arrayDiff(inputwork.oldKeys, keys)), inputwork.repeatCount].concat(keys));
if (keys[0] == 0)
inputwork.keyChanged = false;
if (keyChanged) {
//callback(["key_change", keysToStr(arrayDiff(keys, inputwork.oldKeys)), inputwork.repeatCount].concat(keys));
//serial.println(`$ oldkeys=${inputwork.oldKeys}; newkeys=${keys}`)
//serial.println(`$ keydiff=${arrayDiff(keys, inputwork.oldKeys)}; newkeysym=${keysToStr(arrayDiff(keys, inputwork.oldKeys))}`)
//inputwork.keyChanged = keysToStr(arrayDiff(keys, inputwork.oldKeys));
}
}
else if (inputwork.stroboStatus % 2 == 1 && sys.nanoTime() - inputwork.stroboTime < inputwork.stroboDelays[inputwork.stroboStatus]) {
sys.spin();
@@ -239,7 +273,6 @@ input.withEvent = function(callback) {
}
}
inputwork.oldKeys = keys;
inputwork.oldMouse = mouse;
};

View File

@@ -241,12 +241,12 @@ function hitAny() {
return sys.peek(-41) != 0;
}
function insertChar(code, row, col) {
function insertChar(character, row, col) {
if (textbuffer[row] === undefined)
textbuffer[row] = String.fromCharCode(code);
textbuffer[row] = character;
else {
let s = textbuffer[row].substring(0);
textbuffer[row] = s.substring(0, col) + String.fromCharCode(code) + s.substring(col);
textbuffer[row] = s.substring(0, col) + character + s.substring(col);
}
}
@@ -427,57 +427,64 @@ else {
}
while (!exit) {
let key = con.getch();
input.withEvent(event => {
let eventName = event[0]
if (eventName == "key_down") {
let keysym = event[1]
let keycodes = [event[3],event[4],event[5],event[6],event[7],event[8],event[9],event[10]]
let keycode = keycodes[0]
// TODO: either implement the new strobing-getch() by yourself or modify the sys.getch() so that CTRL-alph would return correct numbers
// serial.println(`getch = ${key}`)
if (bulletinShown) dismissBulletin();
if (key == 17) // Ctrl-Q
if (keysym == "q" && keycodes[1] == 129) // Ctrl-Q
exit = true;
else if (key == 19 && !bulletinShown) {
else if (keysym == "s" && keycodes[1] == 129 && !bulletinShown) {
writeout();
displayBulletin(`Wrote ${textbuffer.length} lines`);
}
else if (key == con.KEY_BACKSPACE) { // Bksp
else if (keycode == 67) { // Bksp
backspaceOnce();
drawLnCol(); gotoText();
}
else if (key == con.KEY_RETURN) { // Return
else if (keycode == 66) { // Return
appendLine(); drawLnCol(); gotoText();
}
else if (key == con.KEY_LEFT) {
else if (keysym == "<LEFT>") {
cursoringCol = cursorCol - 1;
if (cursoringCol < 0) cursoringCol = 0;
cursorMoveRelative(-1,0);
}
else if (key == con.KEY_RIGHT) {
else if (keysym == "<RIGHT>") {
cursoringCol = cursorCol + 1;
if (cursoringCol > textbuffer[cursorRow].length) cursoringCol = textbuffer[cursorRow].length;
cursorMoveRelative(1,0);
}
else if (key == con.KEY_UP) {
else if (keysym == "<UP>") {
cursorMoveRelative(0,-1);
}
else if (key == con.KEY_DOWN) {
else if (keysym == "<DOWN>") {
cursorMoveRelative(0,1);
}
else if (key == con.KEY_PAGE_UP) {
else if (keysym == "<PAGE_UP>") {
cursorMoveRelative(0, -paintHeight + scrollPeek);
}
else if (key == con.KEY_PAGE_DOWN) {
else if (keysym == "<PAGE_DOWN>") {
cursorMoveRelative(0, paintHeight - scrollPeek);
}
else if (key == con.KEY_HOME) {
else if (keysym == "<HOME>") {
cursoringCol = 0;
cursorMoveRelative(-BIG_STRIDE, 0);
}
else if (key == con.KEY_END) {
else if (keysym == "<END>") {
cursoringCol = textbuffer[cursorRow].length;
cursorMoveRelative(BIG_STRIDE, 0);
}
else if (key == con.KEY_TAB) { // insert spaces appropriately
else if (keysym == "<TAB>") { // insert spaces appropriately
let tabsize = TAB_SIZE - (cursorCol % TAB_SIZE);
for (let k = 0; k < tabsize; k++) {
@@ -490,8 +497,8 @@ while (!exit) {
}
drawTextLineAbsolute(cursorRow, scrollHor); drawLnCol(); gotoText();
}
else if (key >= 32 && key < 128) { // printables (excludes \n)
insertChar(key, cursorRow, cursorCol);
else if (keysym.length == 1 || !keysym.startsWith("<")) { // printables (excludes \n)
insertChar(keysym, cursorRow, cursorCol);
// identical to con.KEY_RIGHT
cursoringCol = cursorCol + 1;
if (cursoringCol > textbuffer[cursorRow].length) cursoringCol = textbuffer[cursorRow].length;
@@ -499,6 +506,8 @@ while (!exit) {
// end of con.KEY_RIGHT
drawTextLineAbsolute(cursorRow, scrollHor); drawLnCol(); gotoText();
}
}})
}
con.clear();

View File

@@ -8,7 +8,7 @@
["1","!"],
["2","@"],
["3","#"],
["4","$)"],
["4","$"],
["5","%"],
["6","^"],
["7","&"],

View File

@@ -8,7 +8,7 @@
["1","!"],
["2","@"],
["3","#"],
["4","$)"],
["4","$"],
["5","%"],
["6","^"],
["7","&"],