Coders Packet

Tracking Mouse Activities using Python

By Faizan Ahmad

This program will keep the track of every activity of a mouse and will print the details in a txt log file. It will specifically print the activity name with coordinates in that file.

Modules being used:-

1. ‘pynput’:- This library allows you to control and monitor input devices. Currently, mouse and keyboard input and monitoring are supported. To install this module, paste the code given below in the terminal or command prompt.

pip install pynput

2. ‘logging’:- logging is part of the Python standard library. It's available as soon as we install Python. We do not need to pip install anything.

Importing the required modules:-

from pynput.mouse import Listener
import logging

 

Functions in the code:-

1. ‘def on_move (x, y)’ :- This function will keep the track of movement of mouse along with change in coordinates. It will be invoked whenever mouse is being moved.

#Function to track movement.
def on_move(x, y):
    logging.info("Mouse moved to ({0}, {1})".format(x, y))

 

2. ‘ def on_click(x, y, button, pressed)’ :- This function will keep the track of mouse click with the coordinates and name of the button getting clicked. It will be invoked only when a mouse button is being clicked.

#Function to track click.
def on_click(x, y, button, pressed):
    if pressed:
        logging.info('Mouse click at ({0}, {1}) with {2}'.format(x, y, button))

 

3.‘def on_scroll(x, y, dx, dy)’ :- This function will keep the track of mouse scroll with the coordinates from where to where it is being scrolled. It will be invoked whenever a mouse is being scrolled.

#Function to track scroll.
def on_scroll(x, y, dx, dy):
    logging.info('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))

 

Driver code:-

logging.basicConfig(filename, level, format) is used for the configuration of the logger. 'filename' will assign the name to the log file and will decide the directory of the log file as well. 'level' will specify the message level of the logs by default. 'format' is the format in which log file will be updated and show the details.

#First we need to configure log file with the format of output.
logging.basicConfig(filename="mouse_log.txt", level=logging.DEBUG, format='%(asctime)s: %(message)s')

 

We are then 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 with .join() method to join it to the main thread.

with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()         #Joining to the main thread.

 

Assembling the codes to understand the flow of the program better:-

from pynput.mouse import Listener
import logging

#First we need to configure log file with the format of output.
logging.basicConfig(filename="mouse_log.txt", level=logging.DEBUG, format='%(asctime)s: %(message)s')

#Function to track movement.
def on_move(x, y):
    logging.info("Mouse moved to ({0}, {1})".format(x, y))

#Function to track click.
def on_click(x, y, button, pressed):
    if pressed:
        logging.info('Mouse click at ({0}, {1}) with {2}'.format(x, y, button))

#Function to track scroll.
def on_scroll(x, y, dx, dy):
    logging.info('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))

with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()         #Joining to the main thread.

 

 

Download project

Reviews Report

Submitted by Faizan Ahmad (fpawn123)

Download packets of source code on Coders Packet