X client receives keys that are not grabbed

Marvin Raaijmakers marvin.nospam at gmail.com
Sat May 6 08:18:58 PDT 2006


I thought that XGrabKey() performs an active grab on the specified key,
but that is wrong (I had to read better). XGrabKey() performs a passive
grab on the keyboard after the specified key is pressed and until it is
released. So the effect I want to reach is done by the following code:


/* First grab some keys using XGrabKey() */
while (1)
{
	XNextEvent (display, &event);
	if (event.type == KeyPress)
	{
		XUngrabKeyboard (display, CurrentTime);
		/* Run a program or simulate key presses using XTestFakeKeyEvent() */
	}
}


- Marvin Raaijmakers

On Fri, 2006-04-21 at 14:42 +0200, Marvin Raaijmakers wrote:

> Hello,
> 
> I wrote keyTouch (http://keytouch.sf.net) which is a program for
> configuring extra function keys. It also includes a program keytouchd
> which is an X client that waits for a key press of an extra function
> key and then executes a program or a plugin.It is an X client without
> a window. And calls XGrabKey for every extra function key. Now I have
> written a plugin that simulates the F-keys (F1-F12) using the XTest
> extension. For some reason this doesn't work, because after calling
> the XTest functions the next XNextEvent call will give a key event of
> the simulated F-key. Yes I know what you are thinking: "he is grabbing
> those F-keys", but I am not. The plugin contains the following code: 
> 
> void
> emulate_key (KeySym keysym)
> /*
> Input:
> 	keysym	- The keysymbol of the key to emulate
> Description:
> 	This function sends a key press and release event of the key with symbol
> 	keysym to the the default X display.
> */
> {
> 	Display	*display;
> 	int	i;
> 	KeyCode	keycode;
> 	
> 	/* Connect to the server specified in the DISPLAY evironment variable */
> 	display = XOpenDisplay(NULL);
> 	if (display == NULL)
> 	{
> 		fprintf (stderr, "Cannot connect to X server %s.\n", XDisplayName(NULL));
> 		return;
> 	}
> 	if (!XQueryExtension (display, "XTEST", &i, &i, &i))
> 	{
> 		fprintf (stderr, "Extension XTest unavailable on display '%s'.\n", XDisplayName(NULL));
> 		XCloseDisplay (display);
> 		return;
> 	}
> 	
> 	keycode = XKeysymToKeycode(display, keysym);
> 	
> 	/* Send the key press event */
> 	XTestFakeKeyEvent(display, keycode, True, 0);
> 	/* Send the key release event */
> 	XTestFakeKeyEvent(display, keycode, False, 0);
> 	XSync (display, False);
> 	XCloseDisplay (display);
> }
> 
> Note that the plugin opens a new connection to the X display for sending the key event, so that the connection of the main application is not used.
> Is it a bug in Xlib or am I doing something wrong?
> 
> Regards,
> Marvin Raaijmakers
> _______________________________________________
> xorg mailing list
> xorg at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/xorg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.x.org/archives/xorg/attachments/20060506/5dcb4434/attachment.html>


More information about the xorg mailing list