Skip to content

About

Educational Python keylogger script using pynput to demonstrate keyboard event capture and file logging.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Python Keylogger

An educational Python script demonstrating keyboard event capture and file logging using the pynput library.

Educational & Authorized-Use Disclaimer: This project is provided strictly for educational purposes, defensive security learning, and authorized testing on systems you own or have explicit permission to monitor. Capturing keystrokes on any device or network without clear authorization may violate applicable laws and organizational policies.

Overview

keylogger.py registers a synchronous keyboard listener via pynput.keyboard.Listener and appends captured key presses to a local text file (key_log.txt) in the current working directory.

Features

  • Printable Key Capture: Logs character keys using key.char inside the on_press callback.
  • Special Key Handling: Catches AttributeError for non-character keys (such as Key.shift, Key.enter, Key.space, etc.) and writes them in the format {key}.
  • Persistent File Appending: Appends all captured keystrokes to key_log.txt as events arrive.

Tech Stack & Repository Structure

  • Language: Python 3
  • Dependency: pynput
python-keylogger/
├── keylogger.py   # Keyboard listener and key_log.txt writer
└── README.md      # Project documentation

Setup & Usage

  1. Clone the repository:

    git clone https://github.com/Ravindra4158/python-keylogger.git
    cd python-keylogger
  2. Install the dependency:

    pip install pynput
  3. Run the script:

    python keylogger.py
  4. Stop the script:

    Press Ctrl+C in the terminal where the script is running. Recorded keystrokes are saved to key_log.txt in the current working directory.

How It Works

  1. keylogger.py defines log_file = "key_log.txt" and an on_press(key) handler.
  2. On each key press, on_press opens key_log.txt in append mode ("a"):
    • Attempts to write key.char for standard character keys.
    • Falls back to writing {key} when AttributeError is raised for special keys.
  3. Listener(on_press=on_press) starts the keyboard hook and listener.join() keeps the script running until interrupted.

Notes & Limitations

  • Foreground Execution: The script blocks on listener.join() and runs in the active terminal rather than daemonizing in the background.
  • No Built-in Exit Hotkey or Rotation: There is no on_release stop key handler, log rotation, or encryption; output is written as plain text to key_log.txt in the working directory.
  • Platform Permissions: Depending on your operating system and display server (for example, macOS Input Monitoring / Accessibility permissions or Linux input/X11 permissions), pynput may require elevated privileges or explicit OS permissions to observe global keyboard events.
  • Repository Packaging: No requirements.txt or LICENSE file is currently included in the repository.

Screenshots

Keylogger in Terminal

Keylogger in Terminal

Running and Testing

Running and Testing

Log File Output

Log File Output

Contact

About

Educational Python keylogger script using pynput to demonstrate keyboard event capture and file logging.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages