So a bit of an odd project where I'm attempting to use a subprocess to keep track of the number of keys I press to measure my productivity.
Currently I start the subprocess using an Amazon Dash button, then kill the process on the second press.
def start_keylogger():
global process
process = subprocess.Popen(["sudo", "python", "test.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def stop_keylogger():
os.killpg(process.pid, signal.SIGUSR1)
process.wait()
key_press_count = process.stdout.read()
return key_press_count
From there my keylogger hasn't been fleshed out quite yet, but I think I would like to use sys.exit()
or exit()
to return the number of key presses.
def exit_and_return_counter():
sys.stdout.write(current_keypress_counter)
exit()
if __name__ == '__main__':
signal.signal(signal.SIGUSR1, exit_and_return_counter)
try:
while 1:
main_loop()
except KeyboardInterrupt:
sys.exit()
Initially I tried to use process.returncode
, but it only returned 1
, I'm assuming the successful exit code. And I can't use stdout, stderr = process.communicate()
unless I want to keep the keylogger a short while after the second Amazon button press.