1、使用GTK中的GdkEvent
GdkEvent *event;
event = gdk_event_new (GDK_KEY_PRESS); //按键按下
event->key.send_event = TRUE;
event->key.keyval = GDK_1;
event->key.window = window;
gtk_main_do_event (event);
gdk_event_free (event);
event = gdk_event_new (GDK_KEY_RELEASE); //按键弹起
event->key.send_event = TRUE;
event->key.keyval = GDK_1;
event->key.window = window;
gtk_main_do_event (event);
gdk_event_free (event);
这种方法并不适合所有情况,例如编辑框显示不了字符,按键消息倒是能收到,不
知道是不是我的程序有问题,欢迎大家指正。
2.使用Xlib
#include
#include
Display *disp = XOpenDisplay (NULL);
keysym = XK_1;
//True为按下,False为弹起
XTestFakeKeyEvent (disp, XKeysymToKeycode (disp, keysym), True,
CurrentTime);
XTestFakeKeyEvent (disp, XKeysymToKeycode (disp, keysym), False,
CurrentTime);
XCloseDisplay (disp);
编译的时候要加上-lXtst选项,这种情况依赖于XLib。
3、直接写键盘设备
#include
/* open keyboard */
fd_kb = open ("/dev/input/event0", O_RDWR);
struct input_event event;
event.code = KEY_1;
event.type = EV_KEY;
event.value = 1; //1表示按下,0表示释放,2表示长按
gettimeofday (&event.time ......
1)#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
int a;
int i;
int flag11;
unsigned char strOutput[8];
struct input_event inmyself;
fd=open("/dev/input/event1",O_RDWR);
if(fd==-1){printf ("open keyboard err:%d\n",fd);}
while(1){
goon: flag11=read(fd, &inmyself, sizeof(inmyself));
if(fd==-1){printf ("read keyboard err:%d\n",fd);}
else
//{for(i=0;i<8;i++)
{
printf ("\n\n####################\n");
printf ("type :%d\n",inmyself.type);
printf ("code :%d\n",inmyself.code);
printf ("value :%d\n",inmyself.value);
printf ("####################\n\n");
}
if (inmyself.value) goto goon;
if(inmyself.code == 1)
break;
//}
}
close(fd);
return 0;
}
阅读(1012) | 评论(0) | 转发(0) |