1
votes

I want to use Python to read raw data from the mouse (e.g. left signal, right signal) independent of the mouse pointer. I don't want to read the position of the cursor, I want to read the raw data, akin to how a game would. How can I do that? I can't find any libraries that support it.

I'm using Windows.

Edit: To clarify, when I said "left signal and right signal", I meant mouse movement, not mouse clicks, Edit 2: I believe the terminology is that I want the "mouse delta." This is how people did it in autohotkey - https://www.autohotkey.com/boards/viewtopic.php?t=10159 - but I want it in Python.

Edit 3: I should mention that this is in a game that constantly resets pointer position, so I can't use the difference in pointer position. That's why I want a lower level API.

2
stackoverflow.com/questions/39235454/… you are not looking hard enough - albusSimba
I should have explained better - when I said left and right signal, I meant mouse movement, not mouse click. I would appreciate it if you also removed the downvote, now I've clarified. - AlexAndHisScripts
stackoverflow.com/questions/44533241/… you can always combine both solutions - albusSimba
You're not listening. I know how to get mouse input like that. I use it all the time. That does not work in my situation (measuring how I move my view in a game.) Using that method it simply shows my cursor as being in the middle of the screen. What I want to see is mouse delta, and pyautogui does not fetch mouse delta. - AlexAndHisScripts
@AlexAndHisScripts have you found a working solution for this? I am working on something that requires the exact same low-level mouse signal data access. pyautogui does not allow for that and only fetches the current mouse position which is useless to me. So is calculating arbitrary deltas because the mouse positions "jumps" back and forth since I'm trying to run my app in conjunction with a 3D PC game. Any response would be highly appreciated. - Zenahr

2 Answers

0
votes

Get the mouse position and compare two events. If the x-axis value increases, the mouse moved right, if the x-axis value decreases, the mouse moved left:

from pynput.mouse import Listener

last_position = None

def on_move(x, y):
    global last_position
    if last_position:
        if x > last_position:
            print('mouse moved right')
        elif x < last_position:
            print('mouse moved left')
    last_position = x

with Listener(on_move=on_move) as listener:
    listener.join()        
0
votes

You can always compare the position constantly that would have easily given you a delta. Any lower level API you will have to code a mouse driver yourself, which is what game developers do to prevent people from scripting, which isn't what python made for.

import pyautogui
import time


while True:
    prev_x, prev_y = pyautogui.position()
    time.sleep(0.3)
    curr_x, curr_y = pyautogui.position()
    if (curr_x - prev_x) > 0:
        print("move right")
    else:
        print("move left")