Read Raw mouse events in bash. Open the /dev/input/mice and get the click event

I did a lot of googling and found it hard to find what I needed. I hope my example is useful to you. I found 3 examples that helped Me. They are covered under cc-wiki attribution, and are included here to keep it simple.

cat <<EOF >> /dev/null
# Copyright (C) 2016
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#Thank you for the examples 
# now for a bash example
# get an event from /dev/input/mice
# linux raw mouse events bash click

# run the following in an xterm and you will see 1 for every mouse click.
# we want just the mouse clicks
while :

    # read 3 bytes from /dev/mouse
    bytesAsBits=$(dd bs=1 count=3 if=/dev/input/mice 2>/dev/null| xxd -b) 

    # bit 8 is left, bit 7 is right,and bit 6 is center 
    leftMouse=$(echo $bytesAsBits|cut -d" " -f2|cut -b8)
    rightMouse=$(echo $bytesAsBits|cut -d" " -f2|cut -b7)
    centerMouse=$(echo $bytesAsBits|cut -d" " -f2|cut -b6)
    echo "L: $leftMouse,  M: $centerMouse,  R: $rightMouse" 


Other Examples

cat <<EOF >> /dev/null

# this is not xbindkeys
# xbindkeys is a program that allows you to launch shell commands with your keyboard or your mouse under X Window. It links commands to keys or mouse #buttons, using a configuration file. It's independant of the window manager and can capture all keyboard keys (ex: Power, Wake...).

You can capture the events using od tool from the /dev/input/mice and then replay them once you have decoded the sequence.

# cat /dev/input/mice | od -t x1 -w3
0000000 08 02 00
0000003 08 08 00
0000006 08 09 00
0000011 08 07 00
0000014 08 04 00
0000017 08 01 01
0000022 08 00 02
0000025 08 02 02
For this you can take help of the python code here:

Get mouse deltas using Python! (in Linux)

L:0, M: 0, R: 0, x: -1, y: -1

L:0, M: 0, R: 0, x: 0, y: -1

L:0, M: 0, R: 0, x: 0, y: -1

L:0, M: 0, R: 0, x: 0, y: 2

L:0, M: 0, R: 0, x: 0, y: 1

L:0, M: 0, R: 0, x: 0, y: -1

L:0, M: 0, R: 0, x: 1, y: -1
Once you have that, you can encode it back into a 3 byte sequence for each mouse move.

To encode the binary values using bash you can refer to this question: Passing binary data as arguments in bash

However I tried and writing to /dev/input/mice does not work.

The reason is that, this file is only giving a streams of events for you which have already happened. So there must be some other way to inject such events.

How to control mouse movement in linux?

shareimprove this answer
edited Dec 24 '13 at 13:59
community wiki
2 revs

tuxdna top 11% overall
I am a Software Engineer by profession and a Free and Open Source Software enthusiast by choice.

And this 

down vote
I'm on a basic device and not having access to X or ... so 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 =;
  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 ):
shareimprove this answer
edited Sep 5 '12 at 17:43

answered Sep 5 '12 at 17:27

Alexandre Mazel
Alexandre Mazel top 48% overall
Software Innovation Director at Aldebaran
Like to program graphics, sounds, robots & fun.


Copyright 2024 Nicholas.A.Schembri