c - 如何在c中获取鼠标滚轮事件

标签 c mouseevent mousewheel scrollwheel

我正在尝试从 /dev/input/mice 文件读取鼠标事件。我能够解析 3 字节鼠标输入以获取三个按钮状态以及 X 和 Y 坐标的增量。但是,向上滚动时的鼠标输入与向下滚动时的鼠标输入相同。如何区分向上滚动事件和向下滚动事件?是否有任何ioctls可以执行任何所需的配置,以便我在这两个事件上从鼠标获得不同的输入?

下面是一个简单的程序,用于在鼠标事件发生时查看鼠标的输入。向上滚动和向下滚动事件会导致该程序打印相同的输出(即 8 0 0)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

int main(void) {

    int mouse_fd = open("/dev/input/mice", O_RDONLY | O_NONBLOCK);
    signed char input[4];
    ssize_t rd_cnt;

    if(mouse_fd < 0)
    {
        perror("Could not open /dev/input/mice");
        exit(EXIT_FAILURE);
    }

    while(true)
    {
        errno = 0;
        rd_cnt = read(mouse_fd, input, 4);
        if(rd_cnt <= 0 && errno != EAGAIN)
        {
            perror("Mouse read error:");
            exit(EXIT_FAILURE);
        }
        else
        {
            for(int i = 0; i < rd_cnt; i++)
            {
                printf("%d", input[i]);
                if(i == rd_cnt - 1)
                {
                    printf("\n");
                }
                else
                {
                    printf("\t");
                }
            }
        }
    }

    return 0;
}

最佳答案

另一种选择是使用 SDL2。

我已经成功地将一个使用 SDL 读取鼠标输入的示例混搭在一起,因此您可以从中获取您喜欢的内容。

#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


char* itoa(int i, char b[]){
    char const digit[] = "0123456789";
    char* p = b;
    if(i<0){
        *p++ = '-';
        i *= -1;
    }
    int shifter = i;
    do{ //Move to where representation ends
        ++p;
        shifter = shifter/10;
    }while(shifter);
    *p = '\0';
    do{ //Move back, inserting digits as u go
        *--p = digit[i%10];
        i = i/10;
    }while(i);
    return b;
}

int main()
{
    //initialize the window
    bool quit = false;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Mouse Events", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    // the event that will occur when a mouse event happens
    SDL_Event event;
    while(!quit)
    {

        while (SDL_PollEvent(&event)) //returns one is there is a pending event (if an event happens)
        {
            switch(event.type)
            {
                case SDL_QUIT: //if the window is exited
                    quit = true;
                    break;

                case SDL_MOUSEBUTTONDOWN:
                    switch (event.button.button)
                    {
                        case SDL_BUTTON_LEFT:
                            SDL_ShowSimpleMessageBox(0, "Click", "Left button was pressed!", window);
                            break;
                        case SDL_BUTTON_RIGHT:
                            SDL_ShowSimpleMessageBox(0, "Click", "Right button was pressed!", window);
                            break;
                    }
                    break;
                case SDL_MOUSEWHEEL:
                    if(event.wheel.y == -1) //negative means the scroll wheel has gone away from the user
                    {
                        SDL_ShowSimpleMessageBox(0, "Wheel Event", "You rolled away from yourself!", window);
                    } else if (event.wheel.y == 1) //vice-versa
                    {
                        SDL_ShowSimpleMessageBox(0, "Wheel Event", "You rolled towards yourself!", window);
                    }

            }

        }

        //do some SDL cleanup
        SDL_Rect dstrect = { 288, 208, 64, 64 };

        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

}

event.wheel 类型可以在这里找到:https://wiki.libsdl.org/SDL_MouseWheelEvent

希望这对你有用!

如果您不想使用 SDL2,可能值得查看该库的源代码以了解它在做什么。

关于c - 如何在c中获取鼠标滚轮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260239/

相关文章:

c - 使用 toupper 函数的 C 程序的奇怪输出

Windows 10 通过鼠标悬停延迟激活窗口?

javascript - Threejs 添加带有轨道控件的鼠标事件

Java Swing JScrollPane 按住 Shift 并使用鼠标滚轮时不滚动

jQuery 工具可使用鼠标滚轮滚动 - 滚动一个位置并停止

c - 在 Visual Studio 中出现错误

c - 在编译器资源管理器中包含 C 的外部头文件

c++ - 为多进程c/c++程序编写共享队列

javascript - 用纯 JavaScript 模拟真实的人类鼠标点击?

c - 如何使用 GTK3 捕捉鼠标滚轮向上/向下事件?