Created
May 29, 2024 10:12
-
-
Save andyg2/0d8d70f433c53d3c758d0524bb0d5e78 to your computer and use it in GitHub Desktop.
Simple windows activity logger - logs keystroke count, mouse pixels and windows size/position per windows title, outputting to a yyyy-mm-dd csv file every 10 seconds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32gui | |
import win32con | |
import win32process | |
import time | |
import csv | |
from datetime import datetime | |
from pynput import keyboard, mouse | |
from collections import defaultdict | |
# Dictionary to store keystrokes and mouse movement per window title | |
activity_data = defaultdict(lambda: {'keystrokes': 0, 'mouse_moves': 0}) | |
current_window_title = None | |
current_date = datetime.now().strftime("%Y-%m-%d") | |
# Initialize CSV file | |
def initialize_csv(): | |
filename = f"{current_date}.csv" | |
with open(filename, mode='a', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerow(["timestamp", "window", "x", "y", "w", "h", "mouse", "kb"]) | |
initialize_csv() | |
def log_to_csv(timestamp, window, x, y, w, h, mouse, kb): | |
global current_date | |
new_date = datetime.now().strftime("%Y-%m-%d") | |
if new_date != current_date: | |
current_date = new_date | |
initialize_csv() | |
filename = f"{current_date}.csv" | |
with open(filename, mode='a', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerow([timestamp, window, x, y, w, h, mouse, kb]) | |
def on_key_press(key): | |
global current_window_title | |
if current_window_title: | |
activity_data[current_window_title]['keystrokes'] += 1 | |
def on_mouse_move(x, y): | |
global current_window_title | |
if current_window_title: | |
activity_data[current_window_title]['mouse_moves'] += 1 | |
def get_current_window_title(): | |
hwnd = win32gui.GetForegroundWindow() | |
return win32gui.GetWindowText(hwnd) | |
# Set up listeners for keyboard and mouse events | |
keyboard_listener = keyboard.Listener(on_press=on_key_press) | |
mouse_listener = mouse.Listener(on_move=on_mouse_move) | |
keyboard_listener.start() | |
mouse_listener.start() | |
while True: | |
hwnd = win32gui.GetForegroundWindow() | |
current_window_title = get_current_window_title() | |
if hwnd and current_window_title: | |
thread_id, process_id = win32process.GetWindowThreadProcessId(hwnd) | |
try: | |
window_style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) | |
new_style = window_style | win32con.WS_BORDER | |
win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE, new_style) | |
except Exception as e: | |
print(f"SetWindowLong failed: {e}") | |
window_text = win32gui.GetWindowText(hwnd) | |
rect = win32gui.GetWindowRect(hwnd) | |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
x, y, w, h = rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1] | |
mouse_moves = activity_data[window_text]['mouse_moves'] | |
keystrokes = activity_data[window_text]['keystrokes'] | |
log_to_csv(timestamp, window_text, x, y, w, h, mouse_moves, keystrokes) | |
# Print the keystrokes and mouse moves for the current window | |
# print(f"Timestamp: {timestamp}") | |
# print(f"Window text: {window_text}") | |
# print(f"Window position and size: {rect}") | |
# print(f"Mouse: {mouse_moves}") | |
# print(f"KB: {keystrokes}") | |
# Reset the counters after logging | |
activity_data[window_text]['mouse_moves'] = 0 | |
activity_data[window_text]['keystrokes'] = 0 | |
else: | |
print("No foreground window found") | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment