Checking my mouse wheel on Linux

This post was written by eli on May 17, 2014
Posted Under: Linux

I had some problem with my mouse wheel (on Microsoft Wireless Mobile mouse 3500, by the way): The scrolling would sometimes be jerky. I was turning the wheel to scroll down, and there were small jumps upwards. How annoying is that?

But how could I know if it’s the mouse’s fault, or maybe a software issue? Look at the raw mice events, of course (as root).

# hexdump -v -e '3/1 "%02x " "\n"' /dev/input/mice

But this is no good. The data that represents the mouse movements appears to be useful, but turning the mouse wheel it says

08 00 00
08 00 00
08 00 00
08 00 00

regardless of whether it’s rolled up or down. That’s because the three-byte PS/2 protocol doesn’t support a mouse wheel. Hmmm…

So the EVDEV interface has to be used.

Program follows, suppose it’s saved as event.c. Note that it filters out anything but mouse wheel events.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/input.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
  int fd;
  struct input_event event_data;

  if((fd = open(argv[1], O_RDONLY)) == -1) {
    perror("opening device");
    exit(EXIT_FAILURE);
  }

  while (read(fd, &event_data, sizeof(event_data))) {
    if ((event_data.code == 8) && (event_data.type == 2) &&
	((event_data.value == 1) || (event_data.value == -1)))
      printf("time %ld.%06ld: Value=%d\n",
	     event_data.time.tv_sec, event_data.time.tv_usec,
	     event_data.value);
  }
  return 0;
}

Compile it with (say)

$ gcc -o event -O3 -Wall event.c

And then, as root, first find which of the event files is related to the mouse by using hexdump and moving the mouse. When choosing the right one, a lot of text appears. In my case, it was

# hexdump -C /dev/input/event6

So now run with the right argument:

./event /dev/input/event6

and in my case I rolled the wheel downwards getting

time 1400332306.969728: Value=-1
time 1400332306.985722: Value=-1
time 1400332307.001774: Value=-1
time 1400332307.018793: Value=-1
time 1400332307.057738: Value=1
time 1400332307.465760: Value=-1
time 1400332307.506717: Value=-1
time 1400332307.521738: Value=-1
time 1400332307.545747: Value=-1
time 1400332307.570738: Value=-1

Aha! Time to buy a new mouse!

Reader Comments

FWIW, it seems you can configure a PS/2 intellimouse to get access to the scrolling wheel. Besides the events approach, configuration tools and/or ioctls should enable access, but here are instructions closer to the metal on which commands give wheel access http://www.computer-engineering.org/ps2mouse/ and a description of the chip interface http://wiki.osdev.org/%228042%22_PS/2_Controller#Second_PS.2F2_Port .

#1 
Written By Jose_X on June 6th, 2014 @ 10:08

Congratulation to this really nice solution!
Thanks a lot for posting!
Best Charly

#2 
Written By DrCWO on August 16th, 2020 @ 17:26

Thank you so much for your solution.

#3 
Written By poiuy on December 5th, 2023 @ 18:04

Add a Comment

required, use real name
required, will not be published
optional, your blog address