0

I am running Fedora linux and I am trying to simulate key strokes in the browser (specifically the arrow keys). What is the best way to do this in C

lodkkx
  • 1,163
  • 2
  • 17
  • 29
  • You can do this using XTest - see [this answer](http://stackoverflow.com/questions/7221600/capturing-display-monitor-images-sending-keyboard-input-on-linux/7221621#7221621) – Flexo Jan 02 '12 at 04:38

1 Answers1

2

You can do this with the XTest extesion, a simple example:

#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/extensions/XTest.h>
#include <unistd.h>

static void SendKey (Display *disp, KeySym keysym)
{
  KeyCode keycode = 0;

  keycode = XKeysymToKeycode (disp, keysym);
  if (keycode == 0) return;

  XTestGrabControl (disp, True);

  XTestFakeKeyEvent (disp, keycode, True, 0);
  XTestFakeKeyEvent (disp, keycode, False, 0);

  XSync (disp, False);
  XTestGrabControl (disp, False);
}

/* Main Function */
int main ()
{
  Display *disp = XOpenDisplay (NULL);

  /* A, B */
  SendKey (disp, XK_A);
  SendKey (disp, XK_B);

  return 0;
}

(adapted from this link)

Flexo
  • 87,323
  • 22
  • 191
  • 272