Skip to content

Instantly share code, notes, and snippets.

@siers
Last active December 12, 2015 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siers/bba9822d26c219d6e912 to your computer and use it in GitHub Desktop.
Save siers/bba9822d26c219d6e912 to your computer and use it in GitHub Desktop.
Obsoleted by xdotool at the time of its creation without me knowing for a couple of years.
// gcc -Wall -Wextra -ggdb -o send send.c
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/keysymdef.h>
#define XK_leftarrow 0x08fb /* U+2190 LEFTWARDS ARROW */
#define XK_uparrow 0x08fc /* U+2191 UPWARDS ARROW */
#define XK_rightarrow 0x08fd /* U+2192 RIGHTWARDS ARROW */
#define XK_downarrow 0x08fe /* U+2193 DOWNWARDS ARROW */
Display* dply;
Window w;
void die(char* cause)
{
printf("%s\n", cause);
exit(1);
}
void XGetFocusedWindow(Display* dpy, Window* w)
{
int iret;
XGetInputFocus(dpy, w, &iret);
if (*w == None)
die("nofocus");
}
void gen_event(XKeyEvent *e)
{
memset(e, 0, sizeof(*e));
e->type = KeyPress;
e->time = CurrentTime;
e->display = dply;
e->window = w;
e->subwindow = None;
e->root = DefaultRootWindow(dply);
e->x = 1;
e->y = 1;
e->x_root = 1;
e->y_root = 1;
e->same_screen = 1;
}
void set_btn(XKeyEvent *e, KeySym sym)
{
e->keycode = XKeysymToKeycode(dply, sym);
}
int main(int argc, char** argv)
{
int i, len;
KeySym key;
XKeyEvent e;
if (argc != 2 || (dply = XOpenDisplay(NULL)) == NULL)
die("argc / display");
XGetFocusedWindow(dply, &w);
gen_event(&e);
len = strlen(argv[1]);
for (i = 0; i < len; i++) {
key = argv[1][i];
switch (key) {
case '\n':
key = XK_Return;
break;
case '\t':
key = XK_Return;
break;
case '<':
key = XK_leftarrow;
break;
case '>':
key = XK_rightarrow;
break;
case 'A':
key = XK_Page_Up;
break;
case 'V':
key = XK_Page_Down;
break;
case '+':
key = XK_F11;
break;
}
set_btn(&e, key);
e.type = KeyPress;
XSendEvent(dply, w, 1, KeyPressMask, (XEvent*) &e);
XFlush(dply);
e.type = KeyRelease;
XSendEvent(dply, w, 1, KeyReleaseMask, (XEvent*) &e);
XFlush(dply);
usleep(250);
}
XCloseDisplay(dply);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment