Python Keylogger


Massachusetts Institute of Technology


June 13, 2020

I have been struggling a lot with pain in my fingers and wrists1.

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 this library is safe enough (read: blind faith in open-source) . 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:

Check the Code
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 =
def on_press(key):

def key_count(key):
    global today, day_count, update_every
    if today ==
        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
        # update today's value
        today =
        print("Today is a brand new day :)")
        # reset the counter
        day_count = 0

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

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


  1. It’s 2023 and I still struggle with this. Buy a proper keyboard and pay attention to ergonomics. Physical Therapy helps. Get help. Take care of yourself.↩︎



BibTeX citation:
  author = {Andina, Matias},
  title = {Python {Keylogger}},
  date = {2020-06-13},
  url = {},
  langid = {en}
For attribution, please cite this work as:
Andina, Matias. 2020. “Python Keylogger.” June 13, 2020.

Enjoy my creations?

I'm so glad you're here. As you know, I create a blend of fiction, non-fiction, open-source software, and generative art - all of which I provide for free.

Creating quality content takes a lot of time and effort, and your support would mean the world to me. It would empower me to continue sharing my work and keep everything accessible for everyone.

How can you support my work?

There easy ways to contribute. You can buy me coffee, become a patron on Patreon, or make a donation via PayPal. Every bit helps to keep the creative juices flowing.

Become a Patron!

Share the Love

Not in a position to contribute financially? No problem! Sharing my work with others also goes a long way. You can use the following links to share this post on your social media.

Affiliate Links

Please note that some of the links above might be affiliate links. At no additional cost to you, I will earn a commission if you decide to make a purchase.

© CC-By Matias Andina, 2023 | This page is built with ❤️ and Quarto.