简体   繁体   中英

Mouse motion tracking Program with python

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 ?

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)

ps 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM