spock
10/7/2016 - 5:39 PM

Gets mouse delta as (x, y) tuple of coordinates.

Gets mouse delta as (x, y) tuple of coordinates.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pymouse import PyMouse, PyMouseEvent
from threading import Thread
import numpy


class DetectMouseClick(PyMouseEvent):
    def __init__(self):
        PyMouseEvent.__init__(self)
        self.mouse = PyMouse()
        self.start_xy = (0, 0)

    def print_message(self):
        while self.do == 1:
            pass

    def click(self, x, y, button, press):
        if button == 1:                   # left button, customize if required
            if press:
                self.do = 1                
                self.start_xy = self.mouse.position()               
                self.thread = Thread(target=self.print_message)
                self.thread.start()

            else:
                self.do = 0                
                stop_xy = self.mouse.position()
                delta_xy = tuple(numpy.subtract(stop_xy, self.start_xy))  # delta_xy        
                print("Delta X, Y: {}".format(delta_xy))      

        else:
            self.do = 0

            
# run mouse event listener with:
O = DetectMouseClick()
O.run()