Created
October 22, 2024 01:58
-
-
Save derekmc/b9c403c03d7930e4c7385f50ca8b6930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dict = qw` | |
a from look say time about get make see to | |
all give man she two also go many so up | |
and have me some use as he more take very | |
at her my tell want be here new than we | |
because him no that well but his not the what | |
by how now their when can I of them which | |
come if on then who could in one there why | |
day into onto these will do it or they with | |
even its other thing would find just our think year | |
first know out this you for like people those your | |
` | |
let chordOrder = [] | |
// keyaddress is how chords are made, | |
// an address is the possible finger positions for | |
// a given finger. | |
// 1: frtg, 2: de, 3: sw, 4: aq | |
// 5: juyh, 6: juyh, 7: ki, 8: ;p | |
let finger_keys = qw`de fgtr sw aq1 ki jhnm lo ;p[` | |
let keyaddress = {} | |
let keys = [] | |
let fingercount = 0 | |
let fingerstate = [0,0,0,0, 0,0,0,0] | |
let freezestate = [0,0,0,0, 0,0,0,0] | |
let maxfingers = 0 | |
let text = '' | |
let textcodes = '' | |
let hexchars = '0123456789ABCDEF' | |
main() | |
function main(){ | |
init() | |
window.addEventListener('keydown', keydown) | |
window.addEventListener('keyup', keyup) | |
} | |
function enumerateChordOrder(){ | |
chordOrder = [] | |
// goes over chords diagonally | |
let diagscore = 2 | |
let diagindex = 0 | |
let j = 0; | |
// max dictionary length | |
// all hex numbers minus all single non-zero digit hex numbers minus the all zero number | |
let n = 16**4 - 15*4 - 1 | |
n = Math.min(n, dict.length) | |
for(let i=0; i<n; ++i){ | |
if(++diagindex == diagscore){ | |
++diagscore | |
diagindex = 1 | |
} | |
let a = diagindex | |
let b = diagscore - a | |
let f = Math.floor | |
let hexstr = '' + hexchars[a%16] | |
hexstr += hexchars[f(a/16)%16] | |
hexstr += hexchars[b%16] | |
hexstr += hexchars[f(b/16)%16] | |
chordOrder.push(hexstr) | |
} | |
console.info('chord order', chordOrder.join(' ')) | |
} | |
function init(){ | |
enumerateChordOrder() | |
keyaddress = {} | |
for(let i=0; i<finger_keys.length; ++i){ | |
let s = finger_keys[i] | |
for(let j=0; j<s.length; ++j){ | |
keyaddress[s[j]] = [i, j+1] | |
} | |
} | |
} | |
function toHex(state){ | |
let result = "" | |
let x = 0 | |
let n = 1 | |
for(let i=0; i<finger_keys.length; ++i){ | |
let m = finger_keys[i].length + 1 | |
if(n*m >= 16){ | |
result += hexchars[x] | |
n = 1 | |
x = 0 | |
} | |
x += (state[i]) * n | |
n *= (m) | |
} | |
if(n > 1){ | |
result += hexchars[x] | |
} | |
return result | |
} | |
function fingerstatus(){ | |
let s = fingerstate.join('') | |
s = 'fingers: ' + s.substr(0,4) + ' ' + s.substr(4) | |
let hex = '0123456789ABCDEF' | |
s += `<br><br>hex code: ${toHex(fingerstate)}` | |
//s += '<br><br>finger-hex: ' + hex[a] + hex[b] + ' ' + hex[d] + hex[c] | |
id('status').innerHTML = s | |
} | |
function keydown(e){ | |
let {key} = e | |
if(key in keyaddress){ | |
let [i, j] = keyaddress[key] | |
if(fingerstate[i] == 0) ++fingercount | |
fingerstate[i] = j | |
} | |
if(fingercount >= maxfingers){ | |
freezestate = fingerstate.slice(0) | |
maxfingers = fingercount | |
} | |
fingerstatus() | |
} | |
function keyup(e){ | |
let {key} = e | |
if(key in keyaddress){ | |
let [i, j] = keyaddress[key] | |
fingerstate[i] = 0 | |
--fingercount | |
if(fingercount == 0){ | |
if(maxfingers > 1) | |
emit(freezestate) | |
else | |
input(key) | |
maxfingers = 0 | |
} | |
} else { | |
input(key) | |
} | |
fingerstatus() | |
} | |
function writeTextCodes(x, y){ | |
while(x.length > y.length) y += ' ' | |
while(y.length > x.length) x += ' ' | |
text += x | |
textcodes += y | |
let out = id('textout') | |
let codeout = id('codeout') | |
out.innerHTML = '' | |
out.appendChild(document.createTextNode(text)) | |
codeout.innerHTML = '' | |
codeout.appendChild(document.createTextNode(textcodes)) | |
} | |
function emit(state){ | |
let a = toHex(state) | |
let i = chordOrder.indexOf(a) | |
let b = i>=0? dict[i] : a | |
writeTextCodes(' ' + b + ' ', ' ' + a + ' ') | |
} | |
function input(key){ | |
if(key.length == 1) | |
writeTextCodes(key, key) | |
else | |
console.log(key) | |
//console.log(key) | |
} | |
function qw(parts){ | |
return parts.join('').trim().split(/\s+/) | |
} | |
function id(x){ | |
return document.getElementById(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment