您如何从/dev/input/mice读取鼠标按钮状态? [英] How do you read the mouse button state from /dev/input/mice?

查看:712
本文介绍了您如何从/dev/input/mice读取鼠标按钮状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从/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.

推荐答案

您可以打开设备并从中读取信息. /dev/input/mice中的事件为3个字节长,需要进行一些解析.我认为现在首选的方法是改用/dev/input/event#.但是,这是一个使用/dev/input/mice的小示例.

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; 
}

单击鼠标即可生成:

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

这篇关于您如何从/dev/input/mice读取鼠标按钮状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆