78 lines
1.8 KiB
JavaScript
78 lines
1.8 KiB
JavaScript
const { exec } = require("child_process");
|
|
const keys = require('./keys')
|
|
const fs = require('fs')
|
|
|
|
var ding, savedLog = [], log = []
|
|
|
|
function emptyLog(i = 0){
|
|
for(var i=0; i<256; i++){
|
|
log[i] = 0
|
|
savedLog[i] = {
|
|
key: keys[i],
|
|
count: 0
|
|
}
|
|
}
|
|
}
|
|
|
|
function onPress(data){
|
|
data = data.split(' ')
|
|
if(data[1] == 'press'){
|
|
keyCode = data[data.length-2]
|
|
|
|
//console.log(`${keys[keyCode]} (${keyCode}): ${log[keyCode]}`)
|
|
|
|
log[keyCode] += 1;
|
|
|
|
if(typeof keys[keyCode] == 'undefined' || keys[keyCode].split(' ')[0] == '[unknown'){
|
|
console.log(`unknown key pressed: ${keys[keyCode]} (${keyCode})`)
|
|
}
|
|
}
|
|
}
|
|
|
|
function startLog(){
|
|
saveLog()
|
|
ding = exec("xinput test 10")
|
|
ding.stdout.on('data', onPress);
|
|
ding.on('close', startLog)
|
|
}
|
|
|
|
function saveLog(){
|
|
var total = 0, data = { date: new Date().getTime(), log: []};
|
|
console.log('Curent numbers')
|
|
for(var i=0; i<log.length; i++){
|
|
// create data structure to save
|
|
savedLog[i].count += log[i]
|
|
data.log[i].count = {
|
|
key: keys[i],
|
|
count: savedLog[i].count
|
|
}
|
|
|
|
// print data to console
|
|
if(log[i] == 0) continue;
|
|
total += log[i]
|
|
console.log(`${keys[i]}: ${log[i]} (${i})`)
|
|
}
|
|
fs.writeFile(`${__dirname}/keystrokes${d.getFullYear()}-${d.getMonth()}-${d.getDate()}.log`, JSON.stringify(data), (err) => {
|
|
if(err) return console.error(err)
|
|
})
|
|
console.log(`total keypresses: ${total}`)
|
|
}
|
|
|
|
function readLog(){
|
|
var d = new Date()
|
|
fs.readFile(`${__dirname}/keystrokes${d.getFullYear()}-${d.getMonth()}-${d.getDate()}.log`, function(err, json){
|
|
if(err) return console.error(err)
|
|
|
|
savedLog = JSON.parse(json).log
|
|
console.log(savedLog)
|
|
})
|
|
}
|
|
|
|
emptyLog()
|
|
readLog()
|
|
startLog()
|
|
|
|
setInterval(function(){
|
|
ding.kill('SIGTERM')
|
|
}, 60*60*1000)
|