Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read the mouse button state from /dev/input/mice?

How do you read the mouse button state from /dev/input/mice? I want to detect if the button is pressed down.

like image 889
JonathanC Avatar asked Jul 12 '12 12:07

JonathanC


People also ask

What are/Dev/input/mouse0 and/dev/input/mice?

What are /dev/input/mouse0 and /dev/input/mice? mousedev is a hack to make legacy programs that use mouse input work. It takes events from either mice or digitizers/tablets and makes a PS/2-style (a la /dev/psaux) mouse device available to the userland.

What is a mousebuttonstate enumeration?

Specifies the possible states of a mouse button. The button is pressed. The button is released. The following example shows a mouse event handler that determines which buttons are currently pressed by checking the button state of each mouse button. The MouseButtonState enumeration specifies constants which correlate to the state of a mouse button.

How does a mouse event handler determine which buttons are currently pressed?

The following example shows a mouse event handler that determines which buttons are currently pressed by checking the button state of each mouse button. The MouseButtonState enumeration specifies constants which correlate to the state of a mouse button.

What happens when a mouse button is down in Linux?

If the mouse cursor is over a window created by another thread, the system will direct mouse input to the specified window only if a mouse button is down. Sets the double-click time for the mouse. A double-click is a series of two clicks of a mouse button, the second occurring within a specified time after the first.


1 Answers

You can open the device and read from it. Events from /dev/input/mice are 3 bytes long and require some parsing. I think the prefered method now is to use /dev/input/event# instead. However, here is a small example using /dev/input/mice.

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

int main(int argc, char** argv)
{
    int fd, bytes;
    unsigned char data[3];

    const char *pDevice = "/dev/input/mice";

    // Open Mouse
    fd = open(pDevice, O_RDWR);
    if(fd == -1)
    {
        printf("ERROR Opening %s\n", pDevice);
        return -1;
    }

    int left, middle, right;
    signed char x, y;
    while(1)
    {
        // Read Mouse     
        bytes = read(fd, data, sizeof(data));

        if(bytes > 0)
        {
            left = data[0] & 0x1;
            right = data[0] & 0x2;
            middle = data[0] & 0x4;

            x = data[1];
            y = data[2];
            printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", x, y, left, middle, right);
        }   
    }
    return 0; 
}

One mouse click generates this:

x=0, y=0, left=1, middle=0, right=0
x=0, y=0, left=0, middle=0, right=0

And one mouse move (Note the "relative" mouse move coordinates):

x=1, y=1, left=0, middle=0, right=0
like image 183
JustinB Avatar answered Sep 24 '22 02:09

JustinB