Skip to content

Instantly share code, notes, and snippets.

@gfwilliams
Created December 9, 2020 13:27
Show Gist options
  • Save gfwilliams/3e68240e07f2b65e107a07c579fc25f7 to your computer and use it in GitHub Desktop.
Save gfwilliams/3e68240e07f2b65e107a07c579fc25f7 to your computer and use it in GitHub Desktop.
var KB = [
'QWERTYUIOP[]',
'ASDFGHJKL;\'#',
'\\ZXCVBNM<>/ '];
var KBX = 3;
var KBY = 160;
var KBS = 20; // key spacing
var KBR = 16; // key size
var keyx = 0, keyy = 0;
function drawKey(x,y,lit) {
var px = KBX+x*KBS,
py = KBY+y*KBS;
g.setColor(lit?-1:0).fillRect(px, py, px+KBR, py+KBR);
g.setColor(lit?0:-1).drawString(KB[y][x],px+2,py)
g.setColor(-1);
}
function drawKB() {
g.setFont("6x8",2);
for (var y=0;y<KB.length;y++)
for (var x=0;x<KB[0].length;x++)
drawKey(x,y,0);
}
g.clear();
drawKB();
drawKey(keyx,keyy,1);
function moveKey(x,y) {
drawKey(keyx,keyy,0); // clear old
keyx += x;
if (keyx>=KB[0].length) keyx=0;
if (keyx<0) keyx=KB[0].length-1;
keyy += y;
if (keyy>=KB.length) keyy=0;
if (keyy<0) keyy=KB.length-1
drawKey(keyx,keyy,1); // show new
}
setWatch(_=>moveKey(0,-1),BTN1,{repeat:true});
setWatch(_=>moveKey(0,1),BTN3,{repeat:true});
setWatch(_=>moveKey(-1,0),BTN4,{repeat:true});
setWatch(_=>moveKey(1,0),BTN5,{repeat:true});
setWatch(_=>{
var key = KB[keyy][keyx];
console.log(`You pressed "${key}"`);
},BTN2,{repeat:true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment