|
发表于 2006-5-23 11:27:13
|
显示全部楼层
XNextEvent阻塞型的,有事件才下去。
[code:1]
#include <stdio.h>
#include <X11/Xlib.h>
create_win(Display *dpy,Window *win,int x, int y, int w, int h)
{
int screen;
screen=DefaultScreen(dpy);
*win = XCreateSimpleWindow(dpy, RootWindow(dpy, screen),
x, y, w, h, 2,BlackPixel(dpy, screen),WhitePixel(dpy, screen));
XSelectInput(dpy,*win,KeyPressMask | KeyReleaseMask | ButtonPressMask
| ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask
| ButtonMotionMask | ExposureMask | StructureNotifyMask | ResizeRedirectMask
| FocusChangeMask |OwnerGrabButtonMask);
XMapWindow(dpy, *win);
XMoveWindow(dpy,*win,x,y);
}
main()
{
Display *dpy;
XImage *image;
Window win;
XEvent event;
int i;
char c;
dpy=XOpenDisplay(NULL);
if(!dpy)
{
printf("XOpenDisplay error\n");
return;
}
create_win(dpy,&win,200,200,400,300);
while(1)
{
XNextEvent(dpy, &event);
switch (event.type)
{
case KeyPress : //按下键盘按键
i = XLookupString(&(event.xkey), &c, 1, NULL, NULL);
printf("KeyPress: %c\n",c);
if(c == 'q')
return;
break;
case KeyRelease : //释放键盘按键
i = XLookupString(&(event.xkey), &c, 1, NULL, NULL);
printf("KeyRelease: %c\n",c);
break;
case ButtonPress: //按下鼠标键
printf("ButtonPress\n");
break;
case ButtonRelease : //释放鼠标键
printf("ButtonRelease\n");
break;
case MotionNotify: //鼠标在窗口上移动
printf("MotionNotify\n");
break;
case EnterNotify: //指针进入窗口
printf("EnterNotify\n");
break;
case LeaveNotify: //指针离开窗口
printf("LeaveNotify\n");
break;
case FocusIn: //窗口得到焦点
printf("FocusIn\n");
break;
case FocusOut: //窗口失去焦点
printf("FocusOut\n");
break;
case ResizeRequest: //窗口大小改变
printf("ResizeRequest\n");
break;
case ConfigureNotify: //窗口移动
printf("ConfigureNotify\n");
break;
default:
printf("%d\n",event.type);
break;
}
}
}
[/code:1] |
|