Discover how to monitor and log Windows usage with Python in this step-by-step guide. From fetching the active window title and process details to continuously tracking and logging changes in real-time, this article walks you through building a simple yet powerful monitoring tool using Python's pywin32 and psutil libraries. Perfect for anyone interested in productivity tracking, system monitoring, or just exploring Python’s interaction with the Windows OS.

Monitoring what happens on your computer can be incredibly useful, whether you’re trying to track your productivity, analyze how you spend your time, or even ensure that certain applications are being used efficiently. Python makes this task approachable and powerful by allowing you to interact directly with the Windows operating system. Let’s dive into the concepts and build a monitoring tool from scratch, step by step, using the interactive Python terminal.

Step 1: Getting the Active Window Title

The first thing we want to do is identify the active window—that is, the window currently in focus on your screen. We’ll use the pywin32 library, which provides a direct interface with Windows APIs. Start by installing the necessary package:

1
2
3
4
5
6
7
# Option 1: Install in virtual environment
python3.exe -m venv .venv
.venv/Scripts/activate
pip install pywin32

# Option 2: Install as user
pip3.exe install --user pywin32

Next, we’ll open the Python terminal and write a simple script to fetch the title of the currently active window.

1
2
3
4
5
6
7
import win32gui

# Get the title of the active window
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)

print(f"Active window title: {title}")

Terminal Output:

1
Active window title: Python 3.9.1 Shell

This code snippet uses win32gui.GetForegroundWindow() to get a handle to the active window and win32gui.GetWindowText() to retrieve the title of that window. When you run this, it will display the title of whatever window is currently active, such as your browser or text editor.

Step 2: Fetching Process Information

Knowing the window title is just part of the picture. We often need more context, like the program associated with the window. This is where the psutil library comes in handy, helping us fetch process-related details.

First, install psutil:

1
pip3.exe install psutil

Now, let’s extend our script to include process information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import win32gui
import win32process
import psutil

# Get the active window's title and process details
window = win32gui.GetForegroundWindow()
_, pid = win32process.GetWindowThreadProcessId(window)
process = psutil.Process(pid)

title = win32gui.GetWindowText(window)
exe_name = process.name()
cmdline = ' '.join(process.cmdline())

print(f"Active window title: {title}")
print(f"Program name: {exe_name}")
print(f"Command line details: {cmdline}")

Terminal Output:

1
2
3
Active window title: Python 3.9.1 Shell
Program name: python.exe
Command line details: python -m idlelib

This script enhances our previous code by adding win32process.GetWindowThreadProcessId(), which fetches the process ID (pid) of the active window. We then use psutil.Process() to grab the process object, allowing us to extract details like the executable name and the command line arguments used to launch it.

Step 3: Monitoring for Changes

Fetching information once is useful, but in many cases, we want to monitor activity over time. For example, we may want to log when we switch between different applications. To do this, we can periodically check the active window and record any changes.

Here’s how to do that using a simple loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import time
import win32gui

last_title = None

while True:
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)

if title != last_title:
print(f"Window switched to: {title}")
last_title = title

time.sleep(1) # Check every second

Terminal Output:

1
2
3
Window switched to: Python 3.9.1 Shell
Window switched to: File Explorer
Window switched to: Google Chrome

This script continuously checks which window is active every second. When it detects a change (i.e., when the title is different from the last one), it prints a message. This is a basic form of monitoring that allows you to keep track of when you switch between applications.

Step 4: Logging Events to a File

Now that we’re detecting when the active window changes, the next step is to log this information so that we can analyze it later. We’ll store the start time, end time, window title, and process details in a CSV file.

Let’s put everything together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import win32gui
import win32process
import psutil
import time
import csv
from datetime import datetime

log_filename = 'window_log.csv'

with open(log_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Start-Time', 'End-Time', 'Window Title', 'Program Name', 'Details'])

last_title = None
start_time = datetime.now()

while True:
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)
_, pid = win32process.GetWindowThreadProcessId(window)
process = psutil.Process(pid)
exe_name = process.name()
cmdline = ' '.join(process.cmdline())

if title != last_title:
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()

if last_title is not None:
with open(log_filename, mode='a', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow([start_time.strftime('%Y-%m-%d %H:%M:%S'),
end_time.strftime('%Y-%m-%d %H:%M:%S'),
last_title, exe_name, cmdline])

print(f"Window switched to: {title}")
last_title = title
start_time = end_time

time.sleep(1) # Check every second

Terminal Output:

1
2
3
Window switched to: Python 3.9.1 Shell
Window switched to: File Explorer
Window switched to: Google Chrome

CSV Output:

1
2
3
4
Start-Time,End-Time,Window Title,Program Name,Details
2024-08-09 12:01:00,2024-08-09 12:05:00,Python 3.9.1 Shell,python.exe,python -m idlelib
2024-08-09 12:05:00,2024-08-09 12:10:00,File Explorer,explorer.exe,C:\Windows\explorer.exe
2024-08-09 12:10:00,2024-08-09 12:15:00,Google Chrome,chrome.exe,C:\Program Files\Google\Chrome\Application\chrome.exe

Wrapping Up

And there you have it—a fully functioning Windows usage monitor written in Python! We started by grabbing the active window title, added process details, and then built a loop to detect changes. Finally, we logged these events to a CSV file for later analysis.

This monitoring tool is a great starting point for a variety of projects, whether you’re interested in tracking your own usage, analyzing how different applications are used across a team, or just learning more about how Python interacts with the Windows OS. The possibilities for extending this are vast—imagine adding features like visualization, alerts, or more detailed event logging.

Engage with these tools, experiment with the code, and see how you can adapt it to your specific needs. Python’s flexibility is what makes projects like this not only feasible but also fun!

Get new posts by email:

Comments

Hint: To type code with Disqus, use
<code><pre>insert.code.here()<pre/><code/>