2

I need to simulate some mouse events and keyboard events for my project and having trouble finding much information about the same.

I tried out the original X11 code for simulating mouse which I got from the stackoverflow post, it seems to work fine but I'm not able to understand the code:

void mouseClick(int button)
{
   Display *display = XOpenDisplay(NULL);

   XEvent event;

   if(display == NULL)
   {
      fprintf(stderr, "Errore nell'apertura del Display !!!\n");
      exit(EXIT_FAILURE);
   }

   memset(&event, 0x00, sizeof(event));

   event.type = ButtonPress;
   event.xbutton.button = button;
   event.xbutton.same_screen = True;

   XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

   event.xbutton.subwindow = event.xbutton.window;

   while(event.xbutton.subwindow)
   {
      event.xbutton.window = event.xbutton.subwindow;

      XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
   }

   if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error\n");

   XFlush(display);

   usleep(100000);

   event.type = ButtonRelease;
   event.xbutton.state = 0x100;

   if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error\n");

   XFlush(display);

   XCloseDisplay(display);
}

so I wanted to ask if there were any simpler abstractions over the xlib library without going through all these as i even need to simulate keyboard actions which could be used with g++.

0

2 Answers 2

0

xdotool is a command line interface that I've used before and it's available in the Fedora repos.

There is also the related libxdo that seems like it would be a more direct aid. For example

xdo_mousemove (const xdo_t * xdo, int x, int y, int screen)

0

XSendEvent doesn't work because many applications discard fake keyboard events by XSendEvent.

Best Way to simulate keyboard events is using XTestFakeKeyEvent api.

Following code will simulate F5 Key press event.

XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_F5), True, 0);
XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_F5), False, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.