[comp.windows.news] Why does X-programs running under XView/NeWS not respond to key-press

andrew@resam.dk (Leif Andrew Rump) (07/24/90)

I have several program written for X which runs perfect under NeWS
except for keyinput - it end in the shell that started the program!
Why?

Leif Andrew Rump, AmbraSoft A/S, Stroedamvej 50, DK-2100 Copenhagen OE, Denmark
UUCP: andrew@ambra.dk, phone: +45 39 27 11 77                /
Currently at Scandinavian Airline Systems                =======/
UUCP: andrew@resam.dk, phone: +45 32 32 22 79                \
SAS, RESAM Project Office, CPHML-V, P.O.BOX 150, DK-2770 Kastrup, Denmark

> > Read oe as: o <backspace> / (slash) and OE as O <backspace> / (slash) < <

siegel@booga.Eng.Sun.COM (Josh Siegel) (07/25/90)

In article <1990Jul23.202347.6625@resam.dk> andrew@resam.dk (Leif Andrew Rump) writes:
>I have several program written for X which runs perfect under NeWS
>except for keyinput - it end in the shell that started the program!
>Why?
>

OpenWindows requires window manager hints to be sent to the 
window manager in order to get keyboard events.  Below is a sample
program that "should" compile under OpenWindows 2.0 that gives
a example of keyboard events.

Hope this helps

  --josh siegel

---

/* A side note...
 *
 *   For keyboard input, you MUST give the XSetWMHints() call BEFORE
 *   you map the window.  If you do it after, it has no effect.
 */

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

main(argc, argv)

int	argc;
char	*argv[];
{
	GC	gc;
	KeySym	key;
	XEvent	event;
	Window	window;
	Display	*display;

	XSizeHints shints;
	XWMHints   wmhints;
	XGCValues  values;

	char	 temp;
	int	 num, screen;
	unsigned long mask, foreground, background;


	if (!(display = XOpenDisplay("")))
	{
	   (void) fprintf(stderr,"Can't open display....\n");
	   exit(1);
	}

	screen	   = DefaultScreen(display);
	foreground = WhitePixel(display, screen);
	background = BlackPixel(display, screen);

	shints.x      =
	shints.y      = 250;
	shints.width  =
	shints.height = 350;
	shints.flags  = PPosition | PSize;


	window = XCreateSimpleWindow(display, DefaultRootWindow(display),
			shints.x, shints.y, shints.width, shints.height,
			2, foreground, background);


	mask      	  = GCForeground | GCBackground;;
	values.foreground = foreground;
	values.background = background;

	gc = XCreateGC(display, window, mask, &values);

	wmhints.input = True;
	wmhints.flags = InputHint;
	XSetWMHints(display, window, &wmhints);

	mask = KeyPressMask  | ExposureMask ;
	XSelectInput(display, window, mask);

	XSetStandardProperties(display, window, "Hello Demo", "Hello Icon",
			       None, argv, &argc, &shints);

	XMapRaised(display, window);

        XFlush(display);
        XSync(display,False);

	while (1)
	{
	    XWindowEvent(display,window, mask,  &event);

	    switch (event.type)
	    {
		case Expose:
		     if (event.xexpose.count == 0)
			XClearWindow(display, window);
		     break;

		case MappingNotify:
		     XRefreshKeyboardMapping(&event);
		     break;

		case KeyPress:
			printf("KeyPress\n");
		     num = XLookupString(&event, &temp, 1, &key, 0);
		     if (num == 1 && (temp == 'q' || temp == 'Q'))
		     {
			XFreeGC(display, gc);
			XDestroyWindow(display, window);
			XCloseDisplay(display);
			exit(0);
		     }
		     break;
	    }
	    XFlush(display);
	}
}

guy@auspex.auspex.com (Guy Harris) (07/26/90)

(This is an X discussion; X programs don't run "under NeWS", although
they run under X11/NeWS.  It's not necessarily Sun-specific, either....)

>OpenWindows requires window manager hints to be sent to the 
>window manager in order to get keyboard events.

There may be other window managers that require this as well.  Note well
the ICCCM's discussion of input focus....