我想单独用Xlib 里面的XSendEvent()接口做一个linux下的一个用java做的软键盘大家都来看看
我到http://tronche.com/gui/x/xlib/event-handling/XSendEvent.html网上面看了下有关XSendEvent()的说明,可我就是不能得到我想要模拟键盘的效果,我把代码发给大家看看.软键盘的界面是我用java swt做的,现在它功能要用低层C来实现。我原来的想法是从java里面得到哪个按键被触发,然后我在把该key对应在linux下的key value(比如XK_1)扔到这个方法里面,然后再把要发送的焦点windowID也扔进去。这样就可以模拟键盘的输入效果。可我都试了好几天没怎么用。哪个能指点我下哦。
这是C文件里面的代码:
#include "Setchar.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xos.h>
#include <X11/cursorfont.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/Xproto.h> /* to get request code */
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Repeater.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SmeLine.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/List.h>
#include <X11/Xmu/WinUtil.h>
#include <X11/extensions/XTest.h>
#include <X11/Xlocale.h>
Window focus_window;
Display *display;
int screen_num;
KeyCode keycode;
static struct KeyMapEntry {
char *config;
KeySym keysym;
} keymap[] = {
{"Escape", XK_Escape},
{"Backspace", XK_BackSpace},
{"1", XK_1},
{"2", XK_2},
{"3", XK_3},
{"4", XK_4},
{"5", XK_5},
{"6", XK_6},
{"7", XK_7},
{"8", XK_8},
{"9", XK_9},
{"0", XK_0},
{"Up", XK_Up},
{"Down", XK_Down},
{"Left", XK_Left},
{"Right", XK_Right},
{"Enter", XK_Return},
{"Shiftr", XK_Shift_R},
{"Shiftl", XK_Shift_L},
{"Ctrlr", XK_Control_R},
{"Ctrll", XK_Control_L},
{"Space", XK_Right},
{"CapsLock", XK_Caps_Lock},
{"Tab", XK_Tab},
{"F1", XK_F1},
{"F2", XK_F2},
{"F3", XK_F3},
{"F4", XK_F4},
{"F5", XK_F5},
{"F6", XK_F6},
{"F7", XK_F7},
{"F8", XK_F8},
{"F9", XK_F9},
{"F10", XK_F10},
{"F11", XK_F11},
{"F12", XK_F12},
{"q", XK_q},
{"w", XK_w},
{"e", XK_e},
{"r", XK_r},
{"t", XK_t},
{"y", XK_y},
{"u", XK_u},
{"i", XK_i},
{"o", XK_o},
{"p", XK_p},
{"[", XK_P},
{"]", XK_P},
{"a", XK_a},
{"s", XK_s},
{"d", XK_d},
{"f", XK_f},
{"g", XK_g},
{"h", XK_h},
{"j", XK_j},
{"k", XK_k},
{"l", XK_l},
{";", XK_l},
{"'", XK_Right},
{"z", XK_z},
{"x", XK_x},
{"c", XK_c},
{"v", XK_v},
{"b", XK_b},
{"n", XK_n},
{"m", XK_m},
{",", XK_Right},
{".", XK_Right},
{"/", XK_Right},
{"`", XK_Right},
{"psc", XK_Print},
{"slk", XK_Scroll_Lock},
{"brk", XK_Pause},
{"ins", XK_Insert},
{"hm", XK_Home},
{"pup", XK_Page_Up},
{"del", XK_Delete},
{"end", XK_End},
{"pdn", XK_Page_Down},
{"nlk", XK_Num_Lock},
{"!", XK_Right},
{"@", XK_Right},
{"#", XK_Right},
{"$", XK_Right},
{"%", XK_Right},
{"^", XK_Right},
{"&", XK_Right},
{"*", XK_Right},
{"(", XK_Right},
{")", XK_Right},
{"_", XK_Right},
{"+", XK_Right},
{"|", XK_Right},
{"\\", XK_Right},
{"=", XK_Right},
{"-", XK_Right},
{"\"", XK_Right},
{":", XK_Right},
{"?", XK_Right},
{">", XK_Right},
{"<", XK_Right},
{"keymenu", XK_Menu},
{"metal", XK_Meta_L},
{"metar", XK_Meta_R},
{"Altl", XK_Alt_L},
{"Altr", XK_Alt_R}
};
/*
void set_input_hint(Display *dispay, Window w, Bool bInput)
{
XWMHints *wm_hints;
wm_hints = XGetWMHints(display, w);
wm_hints->flags |= InputHint;
wm_hints->input = bInput;
XSetWMHints(display, w, wm_hints);
XFree(wm_hints);
}
*/
void dispatchEvent(char *msg)
{
printf("\naaafff=%s\n",msg);
XWMHints *wm_hints;
struct KeyMapEntry entry;
int i;
if ( (display=XOpenDisplay(NULL)) == NULL )
{
printf("-2\n");
fprintf( stderr, "winman: cannot connect to \
X server %s\n", XDisplayName(NULL));
exit(-1);
}
printf("-1\n");
screen_num = DefaultScreen(display);
printf("0\n");
focus_window = RootWindow(display, screen_num);
printf("1\n");
wm_hints = XGetWMHints(display, focus_window);
printf("2\n");
wm_hints->flags |= InputHint;
printf("3\n");
wm_hints->input = False;
printf("4\n");
XSetWMHints(display, focus_window, wm_hints);
printf("5\n");
XFree(wm_hints);
for ( i = 0; keymap[i].config; i++) {
entry = keymap[i];
if (strcmp(entry.config, msg) == 0) {
printf("equals is %d\n",strcmp(entry.config, msg));
keycode = XKeysymToKeycode(display, entry.keysym);
XTestFakeKeyEvent(display, keycode, True, 0);
// XSendEvent(display, focus_window, TRUE, KeyPressMask, (XEvent *)&event);
//XSendEvent(display, focus_window, FALSE, NoEventMask, (XEvent *)&event);
XTestFakeKeyEvent(display, keycode, False, 0);
XSync(display, False);
break;
}
}
if (!keymap[i].config)
printf("<WARNING> Unknown config string: %s\n", msg);
XFlush(display);
XCloseDisplay(display);
}
int main(int argc, char *argv[])
{
}
JNIEXPORT void JNICALL Java_Setchar_setchar(JNIEnv * env, jobject jobj, jstring javaString)
{
printf("nativeString= aaa\n");
const jbyte *nativeString = (*env)->GetStringUTFChars(env, javaString, 0);
XSetInputFocus(display, focus_window, RevertToParent, CurrentTime);
printf("nativeString= %s \n ",nativeString );
dispatchEvent((char*)nativeString);
(*env)->ReleaseStringUTFChars(env, javaString, nativeString);
return ;
}