Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!think.com!snorkelwacker.mit.edu!bloom-beacon!dont-send-mail-to-path-lines From: ekberg@asl.dl.nec.COM (Tom Ekberg) Newsgroups: comp.windows.x Subject: XSendEvent problem Message-ID: <9104262018.AA05267@aslss01.asl.dl.nec.com> Date: 26 Apr 91 20:18:43 GMT Sender: daemon@athena.mit.edu (Mr Background) Organization: The Internet Lines: 102 I am trying a simple case of sending a string to a window in attempt to automate a program that runs in an xterm window. The idea is to construct an event with the proper keycode and to send KeyPress and KeyRelease events to the target window. This simple version should send the keycode A to a window, which I can easily extend to the more general case. The problem I am getting is that the target window doesn't see the event. The target windows I have tried are: xterm with allowSendEvents set to True, xev window, and xhw1 window hacked up to allow for key press/release events and to dump the event structure when one is received. In each case, the debug information printed here is displayed and the no errors are detected, but the client does not see the event. The target window ID for XSentEvent is hardcoded (I knew what the ID was), but I have also tried PointerWindow and InputFocus with the same results. Does anyone have any ideas as to why this doesn't work? Enclosed is a complete program which illustrates this problem. ---------------------------------- CUT HERE ---------------------------------- #include #include #include #include main(argc,argv) int argc; char **argv; { Display *dpy; /* X server connection */ XKeyEvent event; /* Event to generate */ Window the_root, mouse_window; int min_keycodes, max_keycodes; int keysyms_per_keycode; KeySym *keycode_to_keysym_ptr, keysym; int keysym_to_keycode[256]; int index, keycode_index; /* * Open the display using the $DISPLAY environment variable to locate * the X server. See Section 2.1. */ if ((dpy = XOpenDisplay(NULL)) == NULL) { fprintf(stderr, "%s: can't open %s\n", argv[0], XDisplayName(NULL)); exit(1); } the_root = XRootWindow(dpy, XDefaultScreen(dpy)); XDisplayKeycodes(dpy, &min_keycodes, &max_keycodes); keycode_to_keysym_ptr = XGetKeyboardMapping(dpy, min_keycodes, max_keycodes - min_keycodes + 1, &keysyms_per_keycode); keycode_index = min_keycodes; /* Hack alert! Construct a table which maps keysyms to keycodes. */ for(index=0; index<(max_keycodes - min_keycodes + 1) * keysyms_per_keycode; index+=keysyms_per_keycode, keycode_index++) { keysym = keycode_to_keysym_ptr[index]; if ((keysym >= XK_space) && (keysym <= XK_asciitilde)) { keysym_to_keycode[keysym] = keycode_index; } } keysym = XK_A; /* Hack alert! Specify the window explicitly. */ mouse_window = 0x600002; fprintf(stderr, "Using keysym %x\n", keysym); fprintf(stderr, "Using keycode %x.\n", keysym_to_keycode[keysym]); event.type = KeyPress; event.display = dpy; event.window = mouse_window; event.root = the_root; event.subwindow = 0; event.x = 16; event.y = 28; event.x_root = 757; event.y_root = 355; event.state = 0; event.keycode = keysym_to_keycode[keysym]; event.same_screen = True; fprintf(stderr, "status=%d.\n", XSendEvent(dpy, mouse_window, False, KeyPressMask, &event)); event.type = KeyRelease; fprintf(stderr, "status=%d.\n", XSendEvent(dpy, mouse_window, False, KeyReleaseMask, &event)); } ------------------------------------------------------------------------------- -- tom, ekberg@asl.dl.nec.com (x3503)