5

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.

  • 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 Aug 30 '20 at 13:06
  • 1
    https://stackoverflow.com/questions/44533241/how-to-print-out-live-mouse-position-coordinates-using-pyautogui you can always combine both solutions – albusSimba Aug 30 '20 at 13:09
  • 1
    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 Aug 30 '20 at 13:13
  • 2
    @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 May 13 '21 at 18:12

2 Answers2

0

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()        
Pascalco
  • 2,481
  • 2
  • 15
  • 31
  • Thanks for the reply. Unfortunately the game (I should have mentioned I'm trying to capture my input to a game, I've edited my post) resets the position constantly. However, I appreciate answering with something that would have worked in the situation I described, even if I didn't describe it correctly and it doesn't work in the actual situation. – AlexAndHisScripts Aug 30 '20 at 13:27
-1

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")
albusSimba
  • 441
  • 4
  • 14