3

I am trying to track mouse motion on a variety of applications, like the desktop, or some web applications. This is to understand and capture user behaviour (those users who are computer illiterate, trying to understand how they behave and interact with the system). For example, if I make such a user sit in front of a desktop and leave him, my program should track all the movements which he makes with the mouse, which I can later correspond with the design of the system.

I wrote a small program in pygame to do the same.

import pygame

x = y = 0
running = 1
screen = pygame.display.set_mode((640, 400))

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    elif event.type == pygame.MOUSEMOTION:
        print "mouse at (%d, %d)" % event.pos

    screen.fill((0, 0, 0))
    pygame.display.flip()

I wish to change the "screen = pygame.display.set_mode((640, 400))". I dont want a new window to be opened by pygame. I want the same window I am working upon, and it tracks the mouse movements. Even if I close my editor, the program should run. There should be no seperate screen. How do I do it ?

sloth
  • 99,095
  • 21
  • 171
  • 219
user3615286
  • 121
  • 1
  • 2
  • 3
  • Related: [Capturing Windows click events with Python](https://stackoverflow.com/questions/7365247/capturing-windows-click-events-with-python), [How can I capture mouseevents and keyevents using python in background on linux](https://stackoverflow.com/questions/12384772/how-can-i-capture-mouseevents-and-keyevents-using-python-in-background-on-linux), [Can I use Python to capture keyboard and mouse events in OSX?](https://stackoverflow.com/questions/9865446/can-i-use-python-to-capture-keyboard-and-mouse-events-in-osx) – sloth May 08 '14 at 08:21

2 Answers2

0

yes you can in this event i have changed your code so that if the mouse is at the coordinate (300,200) then it changes the screen size to (400, 500)

p.s. look at what i added at the beginning:

import pygame
from pygame.locals import *  #just so that some extra functions work
pygame.init() #this turns pygame 'on'

x = y = 0
running = 1
screen = pygame.display.set_mode((640, 400))

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    elif event.type == pygame.MOUSEMOTION:
        print "mouse at (%d, %d)" % event.pos
        if event.pos == (300,200):
            screen = pygame.display.set_mode((400, 500))
    screen.fill((0, 0, 0))
    pygame.display.flip()
marcnetz
  • 113
  • 7
0

I had a similar problem. I realized that pygame can't keylog, or track mouse events, if the window is not in focus, or if the mouse is not currently on the window. If you're looking for a keylogger/ mouse event recorder, try pyHook on pynpnut, depending on if you're using python 2 or 3. These modules can be installed with pip

User 12692182
  • 927
  • 5
  • 16