Created
March 21, 2025 14:53
-
-
Save derekmc/3de5ca21f91122974e7ed4af762a9014 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
window.addEventListener("load", main) | |
let env = { | |
holdtime: 65, | |
} | |
function main(){ | |
test() | |
console.log("Test"); | |
} | |
function log(x){ | |
let s = "" + x | |
let t = document.createTextNode(s) | |
document.body.appendChild( | |
document.createElement("br")) | |
document.body.appendChild(t) | |
console.log(s) | |
} | |
function test(){ | |
let logger = KeyLogger(true) | |
let logtext = "" | |
window.addEventListener("keydown", logger.keydown) | |
window.addEventListener("keyup", logger.keyup) | |
setInterval(flush, 1000) | |
function flush(){ | |
let s = logger.flush() | |
if(s.length){ | |
logtext += s | |
log(s) | |
} | |
} | |
} | |
// key logger allows us to record a keyboard sequence and | |
// interpret it after the fact, so that keyboard implementations | |
// dont need to handle events. | |
function parseLog(s){ | |
let list = s.split(/\s+/) | |
if(list[0].length == 0) list.unshift() | |
let log = [] | |
let t = 0 | |
for(let i=0; i<list.length; ++i){ | |
let entry = list[i] | |
let j = entry.indexOf(":") | |
let action = {"+": "down", "=":"hold", "-":"up"}[entry[0]] | |
let code = j>0? entry.substring(1,j) : entry.substring(1) | |
let time = j>0? parseInt(entry.substring(j+1)) : 0 | |
t += time | |
time = t | |
log.push({action, code, time}) | |
} | |
return log | |
} | |
// log a sequence of keypresses | |
// a short press of space is not sufficient to trigger a chord, | |
// you need a keyhold | |
// even if overlapped | |
// action_code:dt | |
// keydown +112:0000 | |
// keyhold =112:0000 | |
// keyup -112:0000 | |
// | |
function KeyLogger(keeptime){ | |
let ready = "" | |
let pending = "" | |
let down = {} | |
let last = Date.now() | |
let keytimes = {} | |
return {keydown, keyup, flush} | |
function flush(){ | |
let s = ready | |
ready = "" | |
return s | |
} | |
function cleartime(code){ | |
delete keytimes[code] | |
console.log(keytimes) | |
if(Object.keys(keytimes).length == 0){ | |
ready += pending | |
pending = "" | |
} | |
} | |
function delta(){ | |
let now = Date.now() | |
let dt = now - last | |
last = now | |
return dt | |
} | |
function action(symbol, code){ | |
let dt = delta() | |
pending += ' ' + symbol + code + (keeptime? ":" + dt : "") | |
//ready += ' ' + symbol + code + (keeptime? ":" + dt : "") | |
} | |
function keyhold(code, t){ | |
if(keytimes[code] == t){ | |
action("=", code) | |
cleartime(code) | |
} | |
} | |
function keydown(e){ | |
let code = e.keyCode | |
if(keytimes.hasOwnProperty(code)) return | |
//console.log('code', code) | |
let dt = delta | |
action("+", code) | |
keytimes[code] = last | |
setTimeout(keyhold, env.holdtime, code, last) | |
} | |
function keyup(e){ | |
let code = e.keyCode | |
action("-", code) | |
cleartime(code) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment