[comp.windows.x] XSendEvent problem

ekberg@asl.dl.nec.COM (Tom Ekberg) (04/27/91)

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 <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

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)

mikey@sgi.com (Mike Yang) (04/27/91)

One quick suggestion -- do an XSync(dpy, False) before exiting.  Your
X requests are probably not being sent to the server.

-----------------------------------------------------------------------
                 Mike Yang        Silicon Graphics, Inc.
               mikey@sgi.com           415/335-1786

kaleb@thyme.jpl.nasa.gov (Kaleb Keithley) (05/01/91)

In article mikey@sgi.com (Mike Yang) writes:

>One quick suggestion -- do an XSync(dpy, False) before exiting.  Your
>X requests are probably not being sent to the server.
>

Before exiting!  What's wrong with XCloseDisplay(dpy); ?

-- 
Kaleb Keithley                        kaleb@thyme.jpl.nasa.gov

Meep Meep                             Roadrunner
Veep veep                             Quayle