Get Mouse Deltas Using Python! (In Linux)

Get mouse deltas using Python! (in Linux)

I'm on a basic device and not having access to X or ... so event.py doesn't works.

So here's my simpler decode code part to interpret from "deprecated" '/dev/input/mice':

import struct

file = open( "/dev/input/mice", "rb" );

def getMouseEvent():
buf = file.read(3);
button = ord( buf[0] );
bLeft = button & 0x1;
bMiddle = ( button & 0x4 ) > 0;
bRight = ( button & 0x2 ) > 0;
x,y = struct.unpack( "bb", buf[1:] );
print ("L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y) );
# return stuffs

while( 1 ):
getMouseEvent();
file.close();

How to detect mouse click in python 3 on linux?

you can handle mouse input using the lib PyUserInput (code sample from github) :

from pymouse import PyMouseEvent

def fibo():
a = 0
yield a
b = 1
yield b
while True:
a, b = b, a+b
yield b

class Clickonacci(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
self.fibo = fibo()

def click(self, x, y, button, press):
'''Print Fibonacci numbers when the left click is pressed.'''
if button == 1:
if press:
print(self.fibo.next())
else: # Exit if any other mouse button used
self.stop()

C = Clickonacci()
C.run()

otherwise, you can do it with the Xlib lib : Python Xlib catch/send mouseclick

Get mouse deltas under linux (xorg)

If this is for a game, i.e. an application with an actual X window, the typical approach used to be:

  1. Grab the mouse, so all mouse input goes to your window
  2. Warp the mouse pointer to the center of your window, to give maximum amount of space to move
  3. On each mouse movement event, subtract the center of the window from the reported position; this gives you a "delta event"
  4. Goto 2

I write "used to be" because there might be better ways to solve this now, haven't looked into it for a while.

This of course won't give you a resolution that is higher than what X is reporting to applications, i.e. pixels. If you're after sub-pixel reporting, I think you need to go lower, perhaps read the device directly as you suggest.

Python - get raw mouse input

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()

Control mouse by writing to /dev/input/mice

this is not trough the file you mentioned, but its way quicker to use this tool instead of decypering the dump of that file. And it does everything you want in bash.

xdotool does the trick in my terminal.

this is the package site for ubuntu.
you probably can install it trough

# apt-get install xdotool

I could just emerge it on gentoo without adding any repositories.

the tool works fairly simple:

#! /bin/bash
# move the mouse x y
xdotool mousemove 1800 500
# left click
xdotool click 1
# right click
xdotool click 3

found it here



Related Topics



Leave a reply



Submit