当前位置: 技术问答>linux和unix
Red Hat linux 图形界面下模拟键盘事件如何实现?
来源: 互联网 发布时间:2015-12-04
本文导语: 各位高手: 我在 linux下通过程序(c)获得一个字符串,我想把它传给 浏览器中的一个文本输入框.应该怎么实现?是否需要 模拟键盘事件? 各位给点思路,小弟 刚开始C的学习,就要做这个,郁闷阿 ...
各位高手:
我在 linux下通过程序(c)获得一个字符串,我想把它传给 浏览器中的一个文本输入框.应该怎么实现?是否需要 模拟键盘事件?
各位给点思路,小弟 刚开始C的学习,就要做这个,郁闷阿
我在 linux下通过程序(c)获得一个字符串,我想把它传给 浏览器中的一个文本输入框.应该怎么实现?是否需要 模拟键盘事件?
各位给点思路,小弟 刚开始C的学习,就要做这个,郁闷阿
|
这里有一个例子仅供参考! 这个programme完成的功能是在终端下实现键盘输入~~
/* key.c */
/* mapping keycode-keysym */
#include
#include
#include
#include
GC gc;
Display *display;
int screen;
Window win, root;
unsigned long white_pixel, black_pixel;
int nbre_keysym, i, minkc, maxkc, nbrekc;
main() {
display = XOpenDisplay ("");
XDisplayKeycodes (display, &minkc, &maxkc);
nbrekc = maxkc - minkc + 1 ;
printf("%d keycodes de %d a %dn", nbrekc, minkc, maxkc);
XGetKeyboardMapping(display, minkc, nbrekc, &nbre_keysym);
printf("nombre maximal de keysyms par keycode : %dn", nbre_keysym);
screen = DefaultScreen (display);
gc = DefaultGC (display, screen);
root = RootWindow (display, screen);
white_pixel = WhitePixel (display, screen);
black_pixel = BlackPixel (display, screen);
win = XCreateSimpleWindow (display, root, 0, 0,
200, 50, 2, black_pixel, white_pixel);
XSelectInput (display, win, ExposureMask | ButtonPressMask | KeyPressMask);
XStoreName (display, win, "key");
XMapRaised (display, win);
for (;;) {
XEvent ev;
KeySym touche, keysym;
char caractere[10], modifiers[40];
int nbre;
XNextEvent (display, &ev);
switch (ev.type) {
case Expose :
XDrawString (display, win, gc, 10, 10,
"Enfoncez une touche du clavier", 30);
XDrawString (display, win, gc, 10, 25,
"avec touches modifiers ou sans", 30);
break;
case ButtonPress :
exit(0);
case KeyPress :
nbre = XLookupString(&ev, caractere, 10, &touche, 0);
caractere[nbre] = '';
printf("Le keycode est %d (0x%x), le keysym est %d (0x%x)n",
ev.xkey.keycode, ev.xkey.keycode,
XKeysymToString(touche), touche);
if (nbre == 0)
printf("La chaine ascii correspondante est viden");
else
printf("La chaine ascii obtenue est %sn", caractere);
modifiers[0] = '';
if (ev.xkey.state & ShiftMask)
strcat(modifiers, "Shift ");
if (ev.xkey.state & LockMask)
strcat(modifiers, "Lock ");
if (ev.xkey.state & ControlMask)
strcat(modifiers, "Control ");
if (ev.xkey.state & Mod1Mask)
strcat(modifiers, "Mod1 ");
if (ev.xkey.state & Mod2Mask)
strcat(modifiers, "Mod2 ");
if (strlen(modifiers) == 0)
printf("Aucun modifier actifn");
else
printf("Modifiers actifs %sn", modifiers);
printf("Liste des keysyms associes : ");
for (i = 0; i
/* key.c */
/* mapping keycode-keysym */
#include
#include
#include
#include
GC gc;
Display *display;
int screen;
Window win, root;
unsigned long white_pixel, black_pixel;
int nbre_keysym, i, minkc, maxkc, nbrekc;
main() {
display = XOpenDisplay ("");
XDisplayKeycodes (display, &minkc, &maxkc);
nbrekc = maxkc - minkc + 1 ;
printf("%d keycodes de %d a %dn", nbrekc, minkc, maxkc);
XGetKeyboardMapping(display, minkc, nbrekc, &nbre_keysym);
printf("nombre maximal de keysyms par keycode : %dn", nbre_keysym);
screen = DefaultScreen (display);
gc = DefaultGC (display, screen);
root = RootWindow (display, screen);
white_pixel = WhitePixel (display, screen);
black_pixel = BlackPixel (display, screen);
win = XCreateSimpleWindow (display, root, 0, 0,
200, 50, 2, black_pixel, white_pixel);
XSelectInput (display, win, ExposureMask | ButtonPressMask | KeyPressMask);
XStoreName (display, win, "key");
XMapRaised (display, win);
for (;;) {
XEvent ev;
KeySym touche, keysym;
char caractere[10], modifiers[40];
int nbre;
XNextEvent (display, &ev);
switch (ev.type) {
case Expose :
XDrawString (display, win, gc, 10, 10,
"Enfoncez une touche du clavier", 30);
XDrawString (display, win, gc, 10, 25,
"avec touches modifiers ou sans", 30);
break;
case ButtonPress :
exit(0);
case KeyPress :
nbre = XLookupString(&ev, caractere, 10, &touche, 0);
caractere[nbre] = '';
printf("Le keycode est %d (0x%x), le keysym est %d (0x%x)n",
ev.xkey.keycode, ev.xkey.keycode,
XKeysymToString(touche), touche);
if (nbre == 0)
printf("La chaine ascii correspondante est viden");
else
printf("La chaine ascii obtenue est %sn", caractere);
modifiers[0] = '';
if (ev.xkey.state & ShiftMask)
strcat(modifiers, "Shift ");
if (ev.xkey.state & LockMask)
strcat(modifiers, "Lock ");
if (ev.xkey.state & ControlMask)
strcat(modifiers, "Control ");
if (ev.xkey.state & Mod1Mask)
strcat(modifiers, "Mod1 ");
if (ev.xkey.state & Mod2Mask)
strcat(modifiers, "Mod2 ");
if (strlen(modifiers) == 0)
printf("Aucun modifier actifn");
else
printf("Modifiers actifs %sn", modifiers);
printf("Liste des keysyms associes : ");
for (i = 0; i