简体   繁体   English

在Linux上跟踪C中的键盘和鼠标事件

[英]Track keyboard and mouse events in C on linux

How can I track the keyboard or mouse events in Linux, in C? 如何在C中跟踪Linux中的键盘或鼠标事件?

Like for example if the user presses ESC Shift etc. I should be able to track it. 例如,如果用户按下ESC Shift等,我应该能够跟踪它。 Same way for the mouse. 鼠标的方式相同。 If the user moves the mouse or clicks left or right. 如果用户移动鼠标或向左或向右单击。

The implementation idea is to create a small screen saver with timer and I am struggling how to track the keyboard or mouse events to reset the timer. 实现的想法是创建一个带有计时器的小屏幕保护程序,我正在努力如何跟踪键盘或鼠标事件以重置计时器。

One possibility is to use the Input Subsystem. 一种可能性是使用输入子系统。 Take a look at this article: Using the Input Subsystem ( http://www.linuxjournal.com/article/6429 ) 看看这篇文章:使用输入子系统( http://www.linuxjournal.com/article/6429

Another one is to create a working thread that try to read the file /dev/input/event* like eg here for keyboard: 另一个是创建一个工作线程,尝试读取文件/ dev / input / event *,例如这里的键盘:

// (const char *)ptr - pass your device like "/dev/input/event2" here
fd = open((const char *)ptr, O_RDONLY);

if (fd < 0)
{
    fprintf(stderr, "failed to open input device %s: %s\n", (const char *)ptr, strerror(errno));
    return NULL;
}

struct timeval escapeDown = { 0, 0};
int code;
while (1)
{
    if (read(fd, &ev, sizeof(struct input_event)) < 0)
    {
        fprintf(stderr, "failed to read input event from input device %s: %s\n", (const char *)ptr, strerror(errno));
        if (errno == EINTR)
            continue;
        break;
    }

    code = -1;
    if (ev.type == EV_KEY)
    {
        switch (ev.code)
        {
        case eEsc:
            if (ev.value == 1)
            {
                escapeDown = ev.time;
                printf("DOWN: ESC\n");
            }
            else if (ev.value == 0 && escapeDown.tv_sec)
            {
                printf("UP:   ESC\n");
                if (isLongPressed(&escapeDown, &ev.time))
                    code = eEscLong;
                else
                    code = eEsc;

                escapeDown.tv_sec  = 0;
                escapeDown.tv_usec = 0;
            }
            break;
        case eOk:
        case eUp:
        case eRight:
        case eLeft:
        case eDown:
            if (ev.value == 0)
            {
                printf("UP:   %s\n", keyName(ev.code));
                code = ev.code;
            }
            else if (ev.value == 1)
            {
                printf("DOWN: %s\n", keyName(ev.code));
            }
            escapeDown.tv_sec  = 0;
            escapeDown.tv_usec = 0;
            break;
        default:
            break;
        }
    }
    if (code > 0)
    {
        struct sMsg* pMsg = malloc(sizeof(struct sMsg));
        if (pMsg)
        {
            pMsg->nMsgType = eMsgKeyLogger;
            pMsg->nIntValue= code;
            postMsg(pMsg);
        }
        printf("generated keyboard event: %u %s\n",
               code,
               keyName(code));
    }
    else
        usleep(100);
}

close(fd);

Considering the size and nature of your project, you might want to have a look at GLUT . 考虑到项目的大小和性质,您可能需要查看GLUT It is actually a convenience library for OpenGL, but also provides easy-to-use cross-platform input handling and timer functionality. 它实际上是OpenGL的便利库,但也提供易于使用的跨平台输入处理和计时器功能。 Just in case that you want to move to other platforms in the future. 以防万一您希望将来迁移到其他平台。 Other than that, it blends in well with the graphical nature of your application. 除此之外,它与您的应用程序的图形性质完美融合。

Edit: The project I linked is actually a successor to the original GLUT with an overall enhanced API. 编辑:我链接的项目实际上是原始GLUT的继承者,具有整体增强的API。 For the original API reference, look here . 有关原始API参考,请查看此处

In your case, you could use something like: 在您的情况下,您可以使用以下内容:

void keyboardFunc(unsigned char key, int x, int y)
{
    switch (key)
    {
    case 'a':
        break;
    /* etc */
    }
}

void displayFunc()
{
    /* Statements issuing the drawing of your screensaver */
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);

    /* Other initialization code */

    glutKeyboardFunc(keyboardFunc);
    glutDisplayFunc(displayFunc);

    glutMainLoop();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM