How to know the direction of the mouse?

Asked

Viewed 165 times

2

I would like to make a program that indicates to which position the mouse is moving (right, left, high, low, right and high, right and low...), but I can’t get a logic that gives me this result.

Examples are: If the Y and X are being incremented, then the mouse will be going up and right at the same time. If only X is being incremented, then the mouse is going right. If X is being decremented, the mouse is going left. And so on and so forth.

Amostra

I’ve thought of many ways, but nothing works.

import pynput

def Position():
    mouse = pynput.mouse.Controller()
    while True:
        position_anterior = mouse.position[0] #mouse.position retorna uma tupla com (x, y)
        position_atual = mouse.position[0]
        if position_atual > position_anterior: #
            print("direita")
        elif position_atual < position_anterior:
            print("esquerda")
        elif position_atual == position_anterior:
            print("parado")
Position()

1 answer

3


You have to compare with the previous position, not twice with the same.

Something like that:

    import pynput

    def Position():
        mouse = pynput.mouse.Controller()
        position_anterior = mouse.position[0]

        while True:
            position_atual = mouse.position[0]
            if position_atual > position_anterior:
                print("direita")
            elif position_atual < position_anterior:
                print("esquerda")
            elif position_atual == position_anterior:
                print("parado")
            position_anterior = position_atual

    Position()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.