Coding A Keylogger In Python
Let's get started. 😎
Head over to your command prompt and type pip install keyboard
.
If the command executed successfully without any error, head over to your favourite IDE. I am going to use sublime text but you can
use any.
Wheeew!!!, We are almost done. Let's say 50% done.
Let me now explain the code. In the first line, we imported the keyboard module, then on the second line, we called the on_press
method of the keyboard class. The on_press
method takes a function as parameter and passes an event to the function. In this code, we called our function logger
. We haven't defined the function yet so if we are to run the code, we will get an error. So we have
to define that function. Noticed the line 3? It's really important, without it, the code won't work. The reason is that, without that statement, our code will execute and close without logging any keys so to prevent the application
from closing, we add the keyboard.wait()
statement to our code. It behaves like a while True
statement.
So now, we have defined the logger
function. Let me explain what the code does, the on_press
method of the keyboard class passes an event to the function logger
. Then logger
then accepts
the event as parameter and prints it out.
If we are to run our code, we will get this. ⬇️
KeyboardEvent(a down)
KeyboardEvent(p down)
KeyboardEvent(p down)
KeyboardEvent(p down)
[Finished in 3.4s]
We don't want the text, we need the letters only so we will have to modify our logger
function. In order to get only the character, we must print out the event.name
not event
so let's modify our
function again.
Now we get the result ⬇️
s
o
y
p
[Finished in 3.1s with exit code 1]
Good, we now get the letters only. We are making a key-logger right? 🤔 So we will need to save the results to a file. In order to make everything simple, let's name a function write
whose job is to write the event.name
value to a text file.
So we are done with our write
function. It takes a parameter called character and writes it's value to a file called logs.txt. Now let's update our logger
function to write the letters or characters to
the text file.
See 😎, I made the logger
function as simple as English.
Let me explain the logger
function again. It takes a parameter event
and passes it's event.name
to the write function which
in-turn writes it to a file.
We are kind of done. Let's see the full code now.
This code runs normally and logs every key. Now a problem arises. Let's see the problem. Run the program and type (Type anywhere, the key-logger will capture the key strokes) "Lucretius is a good boy". When you are done,
close the program and open the logs.txt file. It should be in the directory of the python file.
When we open, we see
lucretiusspaceisspaceaspacegoodspaceboy
But wait 🤔, that's not what we typed.
The problem is from the event.name
value. When we press keys like Enter, the event.name
will be 'enter' or if we press the tab, the event.name
value will be 'tab',
space will be 'space' and etc. In order to solve this problem, we need to filter the value of event.name
before passing it to the write
function.
So let's create a filter
function.
So let me explain the filter
function. It takes a key or character as parameter, if the key is not an alphabet, the function wraps it in square brackets to let you know that it was a 'special-key' not a character typed
by a user. Then after altering the key, it moves out of the condition statement and goes to the write
function and passes the modified key
value to the function.
Again 😫, let's modify the logger
function.
So we are done 😌 💯
Full Code
Once more, let me give a final explanation. We first import the keyboard module.
Then we called the on_press
method of the keyboard class. The on_press
method takes a function as a parameter so we pass the logger
function to the on_press
method. The on_press
method passes an event to the logger
function. The logger
function passes the event.name
to the filter
function
which filters the event.name
. Finally, the filter
function passes the final results to the write
function.