Linux文本模式下监听鼠标事件

Linux文本模式下监听鼠标事件,这里是通过gpm这个来实现的,代码如下:

 1 #include <stdio.h> 
 2 #include <stdlib.h> 
 3 #include <unistd.h> 
 4 #include <gpm.h> 
 5 
 6 void main(int argc,char **argv) 
 7 { 
 8   fd_set readset; 
 9   Gpm_Event event; 
10   Gpm_Connect conn; 
11 
12   conn.eventMask = ~0; 
13   conn.defaultMask = ~GPM_HARD; 
14   conn.maxMod = 0; 
15   conn.minMod = 0; 
16 
17   if (Gpm_Open(&conn,0)==-1) { 
18     printf("Can not open mouse connection\n"); 
19     exit(1); 
20   } 
21 
22 
23   while (1) { 
24     FD_ZERO(&readset); 
25     FD_SET(gpm_fd,&readset); 
26     select(gpm_fd+1,&readset,0,0,0); 
27 
28     if (FD_ISSET(gpm_fd,&readset)) { 
29       if (Gpm_GetEvent(&event)>0) { 
30          printf("mouse: event 0x%02X, at %2i %2i (delta %2i %2i)," 
31            "button %i, modifiers 0x%02X\r\n", 
32            event.type, 
33            event.x,event.y, 
34            event.dx,event.dy, 
35            event.buttons, 
36            event.modifiers 
37         ); 
38       } 
39     } 
40   } 
41   while (Gpm_Close()); 
42 } 

编译:

gcc -o mouse mouse.c -lgmp

如果您的机器没有安装gmp,请先安装gmp,CentOS 6.0可以用下面的命令联网安装:

sudo yum install gpm*

我选择了安装下面这几个库:

gpm
gpm-devel
gpm-static

 

 

注:本文选自http://topic.csdn.net/u/20090407/23/f776fdca-c41b-4478-9fd5-93f61cfbbe92.html,
如有侵犯您的权益,请邮件通知我,我将会在收到通知后尽快删除相关内容。

posted @ 2012-10-22 09:20  ruihong  阅读(1728)  评论(0编辑  收藏  举报