I have been struggling a lot with pain in my fingers and wrists. I have tried setting up timers for using the keyboard. But the only thing that seems to work is reducing the typing (aka get away from the computer and stop using the keyboard!).

Tracking hours didn’t work well, so I guess a last resort to force me is counting the actual number of keys per day and trying to keep that to a minimum.

After a little google searching, I found inspiration on this post: Design a Keylogger in Python.

I wanted something simple. Just count and save to file, in case future me wants to do some sort of analysis. Nothing fancy, no optimization. Most importantly something I can trust is not sending every keystroke I write over the internets.

I will be using pynput library

You can read the pynput docs here.

Of note, pip3 install pynput failed. If only python made it possible for people to install things … 🤷

Anyway, mystery aside, python -m pip install pynput worked. I kind of trust (read blind faith) this library is safe enough. And my passwords are kinda there scrambled somewhere in the text file but I don’t plan to host the file anywhere so it’s reasonably safe (please don’t get remote access to my computer 🙏).

The output to console (again, keep it simple) looks like this

Today is 2020-06-11 and the key count is: 0
...
Today is 2020-06-11 and the key count is: 12000
Today is a brand new day :)
Today is 2020-06-12 and the key count is: 0
Today is 2020-06-12 and the key count is: 1000
Today is 2020-06-12 and the key count is: 2000
...
Today is 2020-06-12 and the key count is: 13000
Today is a brand new day :)
Today is 2020-06-13 and the key count is: 0
Today is 2020-06-13 and the key count is: 1000
Today is 2020-06-13 and the key count is: 2000

For those of you who want to use it, you can find the code below the shameless plug.


I write fiction and non-fiction.
I write open-source software.
I create generative art.

All of these are available for free in different media. If you like what I do, and want me to keep creating, you can contribute using the links below.

Patreon Become a Patron!
Paypal


from pynput.keyboard import Key, Listener
import logging
import os
import datetime

# log_dir defaults to Desktop
log_dir = '/home/matias/Desktop'
# updates every 100 keystrokes
update_every = 1000
day_count = 0
today = datetime.date.today().isoformat()
   
def on_press(key):
    logging.info(str(key))

def key_count(key):
    global today, day_count, update_every
    if today == datetime.date.today().isoformat():
        if day_count % update_every == 0:
            print(f"Today is {today} and the key count is: {day_count}")
        # always update the counter
        day_count = day_count + 1
    else:
        # update today's value
        today = datetime.date.today().isoformat()
        print("Today is a brand new day :)")
        # reset the counter
        day_count = 0
    return

def main():
    logging.basicConfig(filename = (os.path.join(log_dir, "keylog.txt")),
     level=logging.DEBUG,
     format='%(asctime)s: %(message)s')
    with Listener(
        on_press=on_press,
        on_release=key_count) as listener:
        listener.join()

if __name__ == '__main__':
    print("Starting keylogger")
    main()