Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to make a keylogger
antoon (2)

How to make a keylogger

Hello everyone, today you gonna learn how to make a keylogger with pynput. The stupid thing is it doesn't work on repl.it.

Importing Pynput

First, we have to import pynput:

We will also need os in the future. And we're also going to create a list that stores all the pressed keys.

Add a keylistener

Now, we gonna add a keylistener:

Make the on_press() function

The keylistener refers to the on_press() function and it returns the key so we can print the key.

Now it's time to make the on_press() function:

The function will not print the key when shift or enter is pressed.

When backspace is pressed it will remove the last pressed key from the pressed_keys list.

And when space is pressed, '[]' is added to the list to create a new word.

If another key is pressed, the code will add the key to the pressed_keys list and then activate the print_keys() function.

Make the print_keys() function

Let's create the print_keys() function:

The program is ready, so let's test it.

Final Code

So, here is the final code: