<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.8.1">
</HEAD>
<BODY>
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:<BR>
<BR>
<PRE>
/* 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 <FONT COLOR="#000000">XTestFakeKeyEvent</FONT>() */
}
}
</PRE>
<BR>
- Marvin Raaijmakers<BR>
<BR>
On Fri, 2006-04-21 at 14:42 +0200, Marvin Raaijmakers wrote:<BR>
<BLOCKQUOTE TYPE=CITE>
<FONT COLOR="#000000">Hello,</FONT><BR>
<BR>
<FONT COLOR="#000000">I wrote keyTouch (<A HREF="http://keytouch.sf.net">http://keytouch.sf.net</A>) 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: </FONT>
<PRE>
<FONT COLOR="#000000">void</FONT>
<FONT COLOR="#000000">emulate_key (KeySym keysym)</FONT>
<FONT COLOR="#000000">/*</FONT>
<FONT COLOR="#000000">Input:</FONT>
<FONT COLOR="#000000"> keysym - The keysymbol of the key to emulate</FONT>
<FONT COLOR="#000000">Description:</FONT>
<FONT COLOR="#000000"> This function sends a key press and release event of the key with symbol</FONT>
<FONT COLOR="#000000"> keysym to the the default X display.</FONT>
<FONT COLOR="#000000">*/</FONT>
<FONT COLOR="#000000">{</FONT>
<FONT COLOR="#000000"> Display *display;</FONT>
<FONT COLOR="#000000"> int i;</FONT>
<FONT COLOR="#000000"> KeyCode keycode;</FONT>
<FONT COLOR="#000000"> </FONT>
<FONT COLOR="#000000"> /* Connect to the server specified in the DISPLAY evironment variable */</FONT>
<FONT COLOR="#000000"> display = XOpenDisplay(NULL);</FONT>
<FONT COLOR="#000000"> if (display == NULL)</FONT>
<FONT COLOR="#000000"> {</FONT>
<FONT COLOR="#000000"> fprintf (stderr, "Cannot connect to X server %s.\n", XDisplayName(NULL));</FONT>
<FONT COLOR="#000000"> return;</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000"> if (!XQueryExtension (display, "XTEST", &i, &i, &i))</FONT>
<FONT COLOR="#000000"> {</FONT>
<FONT COLOR="#000000"> fprintf (stderr, "Extension XTest unavailable on display '%s'.\n", XDisplayName(NULL));</FONT>
<FONT COLOR="#000000"> XCloseDisplay (display);</FONT>
<FONT COLOR="#000000"> return;</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000"> </FONT>
<FONT COLOR="#000000"> keycode = XKeysymToKeycode(display, keysym);</FONT>
<FONT COLOR="#000000"> </FONT>
<FONT COLOR="#000000"> /* Send the key press event */</FONT>
<FONT COLOR="#000000"> XTestFakeKeyEvent(display, keycode, True, 0);</FONT>
<FONT COLOR="#000000"> /* Send the key release event */</FONT>
<FONT COLOR="#000000"> XTestFakeKeyEvent(display, keycode, False, 0);</FONT>
<FONT COLOR="#000000"> XSync (display, False);</FONT>
<FONT COLOR="#000000"> XCloseDisplay (display);</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000">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.</FONT>
<FONT COLOR="#000000">Is it a bug in Xlib or am I doing something wrong?</FONT>
<FONT COLOR="#000000">Regards,</FONT>
<FONT COLOR="#000000">Marvin Raaijmakers</FONT>
<FONT COLOR="#000000">_______________________________________________</FONT>
<FONT COLOR="#000000">xorg mailing list</FONT>
<FONT COLOR="#000000"><A HREF="mailto:xorg@lists.freedesktop.org">xorg@lists.freedesktop.org</A></FONT>
<FONT COLOR="#000000"><A HREF="http://lists.freedesktop.org/mailman/listinfo/xorg">http://lists.freedesktop.org/mailman/listinfo/xorg</A></FONT>
</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>