Skip to content

Instantly share code, notes, and snippets.

@timonweb
Forked from HaBaLeS/kylog.py
Created October 8, 2013 11:42
Show Gist options
  • Save timonweb/6883437 to your computer and use it in GitHub Desktop.
Save timonweb/6883437 to your computer and use it in GitHub Desktop.
print 'Hello world'
import threading
import time
import sys
print 'test'
"http://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds"
"Decorator which makes an method execute every 'interval' seconds"
def setInterval(interval):
def decorator(function):
def wrapper(*args, **kwargs):
stopped = threading.Event()
def loop(): # executed in another thread
while not stopped.wait(interval): # until stopped
function(*args, **kwargs)
t = threading.Thread(target=loop)
t.daemon = True # stop if the program exits
t.start()
return stopped
return wrapper
return decorator
class KBActivityLogger:
"""Keyboard Activity logger. Logs the amount of Keystrokes every 60 seconds into a File"""
def __init__(self):
self.currentCnt=0
self.running=True
self.logWriter=1
self.writeLog() #start the interval decoration
@setInterval(60)
def writeLog(self):
sys.stdout.write("TickTack")
sys.stdout.flush()
log = open('/tmp/keystrokes.log','a')
log.write(str(time.time())+ ',' + str(self.currentCnt))
log.write('\n')
log.close()
self.currentCnt=0
def start(self):
pipe = open('/dev/input/by-id/usb-046a_0023-event-kbd','r')
while (self.running):
c = pipe.read(24)
evType ='%02X' % ord(c[16])
evCode = '%02X' % ord(c[20])
if(evType== '01' and evCode == '00'):
self.currentCnt=self.currentCnt+1
#sys.stdout.write(str(self.currentCnt))
#sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment