[in.xpert] XSendEvent for demos

cjmchale@cs.tcd.ie (Ciaran McHale) (03/14/91)

In <1991Mar13.164923.18330@usenet@kadsma> pawlicki@kodak.com (ted) writes:


>	How can I simulate a Keystroke to an xterm job? It seems like
>I can use XSendEvent, but I am having trouble getting it to work.

Here's some code that I used in an application that used
XSendEvent():

	XEvent message;

	message.xclient.type = ClientMessage;
	message.xclient.format = 8;  /* MUST be one of {8, 16, 32} */
	strcpy(message.xclient.data.b, "Hope this works");
	message.xclient.window = destination window);
	success = XSendEvent(dpy, destination window), True,
			     NoEventMask, &message);
	if (!success) {
		fprintf(stderr, "SendEvent() failed\n");
		exit(1);
	}


The thing that I found most important was to set the destination
window field in the event structure i.e., the line:

	message.xclient.window = destination window);

If you're writing an Xt application which happens to be sending the
event to a window in your own application (as opposed to another
client) then you can take a different approach. Instead of using
XSendEvent try the following:

	... /* code to set up event structure */
	XtDispatchEvent(event);

i.e., use XtDispatchEvent() instead (but still be sure to set up the
destination window field). This approach also has the advantage of
being faster since you're doing everything locally rather than making
a round trip to the X server.

An important point to note about sending events is that the X server
will force the send_event field to True. Xterm examines this field and
if set will (by default) discard the event rather than process it. This
is to prevent people from "sending" harmful key sequences to an xterm.
For example, sending the string "rm -f *\n" to somebody else's xterm
is not a nice thing to do. You can tell xterm to process events which
have been sent by setting one of its resources (called "allowSendEvents")
to True. This can be done in a resources file or can be toggled on/off
by choosing the appropriate option in the menu displayed by pressing the
<control> key and the 1st mouse button.


Ciaran.
-- 
Ciaran McHale		"Verbosity says it all"			      ____
Department of Computer Science, Trinity College, Dublin 2, Ireland.   \  /
Telephone: +353-1-772941 ext 1538	FAX: +353-1-772204	       \/
Telex: 93782 TCD EI			email: cjmchale@cs.tcd.ie