[comp.windows.x] XSendEvent for demos

pawlicki@kodak.com (ted) (03/14/91)

	This may be a FAQ, but I need help.

	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.

thanks

ted

Ted Pawlicki                          Eastman KODAK Company
pawlicki@kodak.com                    Mailstop : 1/9/EP/35722
(716) 726-7223                        Rochester, New York, 14653-5722

gildea@expo.lcs.mit.EDU (Stephen Gildea) (03/14/91)

    	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.
    
What exactly is your trouble?  If the xterm does not seem to be
getting your events at all, you may not have told it to allow
XSendEvent keystrokes.  (By default this is disabled for security
reasons.)  Set the allowSendEvents resource to true.

 < Stephen
   MIT X Consortium

mikey@eukanuba.wpd.sgi.com (Mike Yang) (03/14/91)

In article <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.

By default, xterm ignores key events that are sent to it using
XSendEvent.  If you want it to recognize such events, set
*allowSendEvents to True.

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

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