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.
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.
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, Listenerimport loggingimport osimport datetime# log_dir defaults to Desktoplog_dir ='/home/matias/Desktop'# updates every 100 keystrokesupdate_every =1000day_count =0today = datetime.date.today().isoformat()def on_press(key): logging.info(str(key))def key_count(key):global today, day_count, update_everyif 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 +1else:# update today's value today = datetime.date.today().isoformat()print("Today is a brand new day :)")# reset the counter day_count =0returndef 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()
Footnotes
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.↩︎
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.
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.