用C跟踪键盘和鼠标事件在Linux上 [英] Track keyboard and mouse events in C on linux

查看:363
本文介绍了用C跟踪键盘和鼠标事件在Linux上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以跟踪Linux的键盘或鼠标事件,在C?

How can I track the keyboard or mouse events in Linux, in C?

像例如,如果用户presses <大骨节病> ESC <大骨节病>移等等。我应该能够跟踪它。为鼠标一样。如果用户移动鼠标或点击左边或右边。

Like for example if the user presses ESC Shift etc. I should be able to track it. 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.

推荐答案

考虑到项目的规模和性质,你可能想看看的 GLUT 。它实际上是对OpenGL便利库,但还提供了易于使用的跨平台的输入处理和定时器功能。以防万一,你要移动到在未来的其他平台。除此之外,它融合以及在与你的应用程序的图形特性。

Considering the size and nature of your project, you might want to have a look at GLUT. It is actually a convenience library for OpenGL, but also provides easy-to-use cross-platform input handling and timer functionality. 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.

编辑:我联系该项目实际上是继承了原来的GLUT与整体增强的API。对于原来的API参考,看看这里

The project I linked is actually a successor to the original GLUT with an overall enhanced API. For the original API reference, look here.

在你的情况,你可以使用类似:

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

这篇关于用C跟踪键盘和鼠标事件在Linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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