简体   繁体   English

带有python的鼠标运动跟踪程序

[英]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.我正在尝试在各种应用程序(例如桌面或某些 Web 应用程序)上跟踪鼠标运动。 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.我在 pygame 中编写了一个小程序来做同样的事情。

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))".我想更改“screen = pygame.display.set_mode((640, 400))”。 I dont want a new window to be opened by pygame.我不希望 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)是的,您可以在这种情况下,我更改了您的代码,以便如果鼠标位于坐标 (300,200) 处,则它将屏幕尺寸更改为 (400, 500)

ps look at what i added at the beginning: ps 看看我在开头添加的内容:

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.我意识到如果窗口不在焦点上,或者鼠标当前不在窗口上,pygame 不能键盘记录或跟踪鼠标事件。 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如果您正在寻找键盘记录器/鼠标事件记录器,请尝试在 pynpnut 上使用 pyHook,具体取决于您使用的是 python 2 还是 3。这些模块可以使用 pip 安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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