A simple key synthesizer using an XSendEvent-ed KeyPress/KeyRelease.

Warning: (u)xterm will ignore key presses generated by XSendEvent, thus
you will not be able to synthesize key presses for such apps. Press
Ctrl+Button1 and hit "Allow SendEvents" to change that for
xterm. Defining the xresource XTerm.*.*.allowSendEvents=true might also
work.

Useful for:
I'm using F2, F3, F4 for ion commands like spawn xterm, run app, or a
lua prompt. I use these function much more often than functions that are
bound by applications to F2, F3, F4.. But I need to access these
function occasionally. Therefor, I use Meta-F2 to synthesize a regular
F2 event. This can be done by

    kpress(MOD1.."F2", "WClientWin.synthesize_key(_sub, 'F2')"),

in the binding map for WMPlex.

Diffed against darcs rep date 20051006. 
-- 
Fruhwirth Clemens - http://clemens.endorphin.org 
for robots: [EMAIL PROTECTED]

--- old-ion-3-1/ioncore/key.c	2005-10-06 09:34:53.000000000 +0200
+++ new-ion-3-1/ioncore/key.c	2005-12-05 13:00:53.000000000 +0100
@@ -22,6 +22,7 @@
 #include <libextl/extl.h>
 #include "strings.h"
 #include "xwindow.h"
+#include "conf-bindings.h"
 
 
 static void waitrelease(WRegion *reg);
@@ -122,6 +123,39 @@
     ioncore_change_grab_cursor(IONCORE_CURSOR_WAITKEY);
 }
 
+/*EXTL_DOC
+ * Synthesizes two XKeyEvents (KeyPress, KeyRelease) to simulate a keypress 
+ * for \var{cwin}. \var{keybut_spec} has the same format as the first 
+ * argument of kpress.
+ */
+EXTL_EXPORT_MEMBER
+void clientwin_synthesize_key(WClientWin *cwin, char *keybut_spec)
+{
+    XKeyEvent ev;
+    uint ksb;
+    uint modifier_state;
+
+    if(!ioncore_parse_keybut(keybut_spec, &ev.state, &ksb, 0, 0))
+      return;
+
+    XQueryPointer(ioncore_g.dpy, cwin->win, 
+		  &ev.root, &ev.subwindow, 
+		  &ev.x_root, &ev.y_root, &ev.x, &ev.y, 
+		  &modifier_state);
+    
+    ev.display = ioncore_g.dpy;
+    ev.window = cwin->win;
+    ev.send_event = True;
+    ev.time = CurrentTime;
+    ev.keycode = XKeysymToKeycode(ioncore_g.dpy, ksb);
+    ev.same_screen = True;
+
+    ev.type = KeyPress;
+    XSendEvent(ev.display, ev.window, True, KeyPressMask, (XEvent *)&ev);
+    ev.type = KeyRelease;
+    XSendEvent(ev.display, ev.window, True, KeyReleaseMask, (XEvent* )&ev);
+}
+
 
 static bool waitrelease_handler(WRegion *reg, XEvent *ev)
 {

Reply via email to