Anton Bychek
Anton Bychek

Reputation: 115

How to capture mouse movements with python?

My main idea is calculate the time of movement of the mouse. I want start python script and take the time from the start. Idk how to do it in clear python, but i read about Qt, it seem helpfull in this task. But i'm never use it, i saw a lot of info about tracking the movement of the mouse, but idk can i calculate the time? How to do it?

Upvotes: 1

Views: 3133

Answers (1)

Kris
Kris

Reputation: 1438

It's not clear what time you want to calculate. The following code will print the speed in pixels per second based on current mouse position and last mouse position on every mouse movement.

import sys
import math
import time

from PyQt5.QtWidgets import QApplication, QMainWindow


def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)


class Frame:
    def __init__(self, position, time):
        self.position = position
        self.time = time

    def speed(self, frame):
        d = distance(*self.position, *frame.position)
        time_delta = abs(frame.time - self.time)
        if time_delta == 0:
            return None
        else
            return d / time_delta


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.last_frame = None
        self.setMouseTracking(True)

    def mouseMoveEvent(self, event):
        new_frame = Frame((event.x(), event.y()), time.time())

        if self.last_frame:
            print(new_frame.speed(self.last_frame))

        self.last_frame = new_frame


if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = MainWindow()
    w.resize(900, 600)
    w.show()

    app.exec_()

EDIT: You could use the following code for tracking mouse movement speed outside of the window on the entire screen, this time in an endless loop instead of on mouse events. However, if you move the mouse back and forth, those distances might cancel each other out if the polling interval is too high.

import sys
import math
import time

from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QApplication


class Frame:
    def __init__(self, position, time):
        self.position = position
        self.time = time

    def speed(self, frame):
        d = distance(*self.position, *frame.position)
        time_delta = abs(frame.time - self.time)
        if time_delta == 0:
            return None
        else:
            return d / time_delta


def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)


def get_current_cursor_position():
    pos = QCursor.pos()
    return pos.x(), pos.y()


def get_current_frame():
    return Frame(get_current_cursor_position(), time.time())


if __name__ == '__main__':
    app = QApplication(sys.argv)

    last_frame = get_current_frame()

    while True:
        new_frame = get_current_frame()
        print(new_frame.speed(last_frame))
        last_frame = new_frame

        time.sleep(0.1)

Upvotes: 2

Related Questions