simulate mouse buttons using Xlib - Xwindows

This is a discussion on simulate mouse buttons using Xlib - Xwindows ; Hi, Is it possible to make a program i C/C++ that simulates mouse button events and movements that works with KDE, MWM and GNOME? I've been able to mouse the mouse around using XWarpPointer and that works for all WM ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: simulate mouse buttons using Xlib

  1. simulate mouse buttons using Xlib

    Hi,

    Is it possible to make a program i C/C++ that simulates mouse button
    events and movements that works with KDE, MWM and GNOME?

    I've been able to mouse the mouse around using XWarpPointer and that
    works for all WM but as sson as I try to send mouse button events it
    doesn't work in GNOME og KDE. The WM detect a mouse down but if I e.g.
    tests this by having 2 terminal windows open the window back is not
    moved to the front as a normal but I can see that the cursor in the
    windows is placed at the right position. I've also tried the
    XTestFakeButtonEvent but without any luck

    I'm using the following code

    #include
    #include
    #include
    #include
    #include

    #include
    #include
    #include


    /************************************************** ********************
    x_press_left_button
    ************************************************** *********************/
    void Test()
    {
    Display *display = XOpenDisplay(NULL);
    XTestFakeButtonEvent(display, 1, True, CurrentTime);
    XTestFakeButtonEvent(display, 1, False, CurrentTime);
    }

    void x_press_left_button()
    {
    Display *display = XOpenDisplay(NULL);
    XEvent event;

    bzero(&event,sizeof(XEvent));
    event.type = ButtonPress;
    event.xbutton.button = Button1;
    event.xbutton.same_screen = True;


    /* get info about current pointer position */
    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;


    /* walk down through window hierachy to find youngest child */
    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);
    }


    /* send ButtonPress event to youngest child */
    if (XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
    printf("XSendEvent Failed\n");
    XFlush(display);


    /* sleep for a little while */
    usleep(10000);


    /* set up ButtonRelease event */
    event.type = ButtonRelease;
    event.xbutton.state = 0x100;


    /* send ButtonRelease event to youngest child */
    if (XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
    printf("XSendEvent Failed\n");
    XFlush(display);
    }


    void mouseMove()
    {
    Display *displayMain = XOpenDisplay(NULL);
    Window fromroot, tmpwin, root;
    int x, y, tmp;
    unsigned int utmp;

    root = DefaultRootWindow(displayMain);

    XQueryPointer(displayMain, root, &fromroot, &tmpwin, &x, &y, &tmp,
    &tmp, &utmp);
    if(displayMain == NULL)
    {
    fprintf(stderr, "Errore nell'apertura del Display !!!\n");
    exit(EXIT_FAILURE);
    }
    for (int i = 0; i < 1024; i++)
    {
    XWarpPointer(displayMain, None, root, 0, 0, 0, 0, i, 200);
    usleep(1000);
    // XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
    XFlush(displayMain);
    }
    x = XDisplayWidth(displayMain, 0);
    y = XDisplayHeight(displayMain, 0);
    XCloseDisplay(displayMain);
    }

    int main(int argc, char **argv)
    {
    mouseMove();
    usleep(3000000);
    x_press_left_button();
    return 0;
    }

  2. Re: simulate mouse buttons using Xlib

    On 2007-01-10, Henrik Thystrup wrote:
    > Hi,
    >
    > Is it possible to make a program i C/C++ that simulates mouse button
    > events and movements that works with KDE, MWM and GNOME?
    > ...


    Have you tried the 'xmacro' package? While I don't use KDE
    or Gnome, I think have used it with mwm before I switched to
    twm.

    HTH

    --
    Robert Riches
    spamtrap42@verizon.net
    (Yes, that is one of my email addresses.)

  3. Re: simulate mouse buttons using Xlib

    nope... thanks for the tip. I'll try it out

    - HTH

    Robert M. Riches Jr. wrote:
    > On 2007-01-10, Henrik Thystrup wrote:
    >> Hi,
    >>
    >> Is it possible to make a program i C/C++ that simulates mouse button
    >> events and movements that works with KDE, MWM and GNOME?
    >> ...

    >
    > Have you tried the 'xmacro' package? While I don't use KDE
    > or Gnome, I think have used it with mwm before I switched to
    > twm.
    >
    > HTH
    >


  4. Re: simulate mouse buttons using Xlib

    int main(int argc, char* argv[])
    {
    Display *display;
    Window root;

    if ((display = XOpenDisplay(NULL)) == NULL) {
    fprintf(stderr, "Cannot open local X-display.\n");
    exit(1);
    }
    int ev, er, ma, mi;
    if(!XTestQueryExtension(display, &ev, &er, &ma, &mi))
    {
    fprintf(stderr, "XTest extension not supported on server.\n");
    exit(1);
    }
    fprintf(stderr,"XTest for server \"%s\" is version %d.%d\n",
    DisplayString(display), ma, mi);
    XTestFakeButtonEvent(display, 1, True, CurrentTime);
    XTestFakeButtonEvent(display, 1, False, CurrentTime);
    XFlush(display);

    Henrik Thystrup:
    > Hi,
    >
    > Is it possible to make a program i C/C++ that simulates mouse button
    > events and movements that works with KDE, MWM and GNOME?
    >
    > I've been able to mouse the mouse around using XWarpPointer and that
    > works for all WM but as sson as I try to send mouse button events it
    > doesn't work in GNOME og KDE. The WM detect a mouse down but if I e.g.
    > tests this by having 2 terminal windows open the window back is not
    > moved to the front as a normal but I can see that the cursor in the
    > windows is placed at the right position. I've also tried the
    > XTestFakeButtonEvent but without any luck
    >
    > I'm using the following code
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > #include
    > #include
    > #include
    >
    >
    > /************************************************** ********************
    > x_press_left_button
    > ************************************************** *********************/
    > void Test()
    > {
    > Display *display = XOpenDisplay(NULL);
    > XTestFakeButtonEvent(display, 1, True, CurrentTime);
    > XTestFakeButtonEvent(display, 1, False, CurrentTime);
    > }
    >
    > void x_press_left_button()
    > {
    > Display *display = XOpenDisplay(NULL);
    > XEvent event;
    >
    > bzero(&event,sizeof(XEvent));
    > event.type = ButtonPress;
    > event.xbutton.button = Button1;
    > event.xbutton.same_screen = True;
    >
    >
    > /* get info about current pointer position */
    > 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;
    >
    >
    > /* walk down through window hierachy to find youngest child */
    > 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);
    > }
    >
    >
    > /* send ButtonPress event to youngest child */
    > if (XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
    > printf("XSendEvent Failed\n");
    > XFlush(display);
    >
    >
    > /* sleep for a little while */
    > usleep(10000);
    >
    >
    > /* set up ButtonRelease event */
    > event.type = ButtonRelease;
    > event.xbutton.state = 0x100;
    >
    >
    > /* send ButtonRelease event to youngest child */
    > if (XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
    > printf("XSendEvent Failed\n");
    > XFlush(display);
    > }
    >
    >
    > void mouseMove()
    > {
    > Display *displayMain = XOpenDisplay(NULL);
    > Window fromroot, tmpwin, root;
    > int x, y, tmp;
    > unsigned int utmp;
    >
    > root = DefaultRootWindow(displayMain);
    >
    > XQueryPointer(displayMain, root, &fromroot, &tmpwin, &x, &y, &tmp,
    > &tmp, &utmp);
    > if(displayMain == NULL)
    > {
    > fprintf(stderr, "Errore nell'apertura del Display !!!\n");
    > exit(EXIT_FAILURE);
    > }
    > for (int i = 0; i < 1024; i++)
    > {
    > XWarpPointer(displayMain, None, root, 0, 0, 0, 0, i, 200);
    > usleep(1000);
    > // XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
    > XFlush(displayMain);
    > }
    > x = XDisplayWidth(displayMain, 0);
    > y = XDisplayHeight(displayMain, 0);
    > XCloseDisplay(displayMain);
    > }
    >
    > int main(int argc, char **argv)
    > {
    > mouseMove();
    > usleep(3000000);
    > x_press_left_button();
    > return 0;
    > }



  5. Re: simulate mouse buttons using Xlib

    this code work

    // g++ test.cpp -L/usr/X11R6/lib -lX11 -lXtst
    #include
    #include
    #include
    #include
    #include
    #include

    int main(int argc, char* argv[])
    {
    Display *display;

    if ((display = XOpenDisplay(NULL)) == NULL) {
    fprintf(stderr, "Cannot open local X-display.\n");
    exit(1);
    }
    int ev, er, ma, mi;
    if(!XTestQueryExtension(display, &ev, &er, &ma, &mi))
    {
    fprintf(stderr, "XTest extension not supported on server.\n");
    exit(1);
    }
    fprintf(stderr,"XTest for server \"%s\" is version %d.%d\n",
    DisplayString(display), ma, mi);
    XTestFakeButtonEvent(display, 1, True, CurrentTime);
    XTestFakeButtonEvent(display, 1, False, CurrentTime);
    XFlush(display);
    return 0;
    }

    Henrik Thystrup:
    > Hi,
    >
    > Is it possible to make a program i C/C++ that simulates mouse button
    > events and movements that works with KDE, MWM and GNOME?
    >
    > I've been able to mouse the mouse around using XWarpPointer and that
    > works for all WM but as sson as I try to send mouse button events it
    > doesn't work in GNOME og KDE. The WM detect a mouse down but if I e.g.
    > tests this by having 2 terminal windows open the window back is not
    > moved to the front as a normal but I can see that the cursor in the
    > windows is placed at the right position. I've also tried the
    > XTestFakeButtonEvent but without any luck
    >
    > I'm using the following code
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > #include
    > #include
    > #include
    >
    >
    > /************************************************** ********************
    > x_press_left_button
    > ************************************************** *********************/
    > void Test()
    > {
    > Display *display = XOpenDisplay(NULL);
    > XTestFakeButtonEvent(display, 1, True, CurrentTime);
    > XTestFakeButtonEvent(display, 1, False, CurrentTime);
    > }
    >
    > void x_press_left_button()
    > {
    > Display *display = XOpenDisplay(NULL);
    > XEvent event;
    >
    > bzero(&event,sizeof(XEvent));
    > event.type = ButtonPress;
    > event.xbutton.button = Button1;
    > event.xbutton.same_screen = True;
    >
    >
    > /* get info about current pointer position */
    > 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;
    >
    >
    > /* walk down through window hierachy to find youngest child */
    > 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);
    > }
    >
    >
    > /* send ButtonPress event to youngest child */
    > if (XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
    > printf("XSendEvent Failed\n");
    > XFlush(display);
    >
    >
    > /* sleep for a little while */
    > usleep(10000);
    >
    >
    > /* set up ButtonRelease event */
    > event.type = ButtonRelease;
    > event.xbutton.state = 0x100;
    >
    >
    > /* send ButtonRelease event to youngest child */
    > if (XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0)
    > printf("XSendEvent Failed\n");
    > XFlush(display);
    > }
    >
    >
    > void mouseMove()
    > {
    > Display *displayMain = XOpenDisplay(NULL);
    > Window fromroot, tmpwin, root;
    > int x, y, tmp;
    > unsigned int utmp;
    >
    > root = DefaultRootWindow(displayMain);
    >
    > XQueryPointer(displayMain, root, &fromroot, &tmpwin, &x, &y, &tmp,
    > &tmp, &utmp);
    > if(displayMain == NULL)
    > {
    > fprintf(stderr, "Errore nell'apertura del Display !!!\n");
    > exit(EXIT_FAILURE);
    > }
    > for (int i = 0; i < 1024; i++)
    > {
    > XWarpPointer(displayMain, None, root, 0, 0, 0, 0, i, 200);
    > usleep(1000);
    > // XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
    > XFlush(displayMain);
    > }
    > x = XDisplayWidth(displayMain, 0);
    > y = XDisplayHeight(displayMain, 0);
    > XCloseDisplay(displayMain);
    > }
    >
    > int main(int argc, char **argv)
    > {
    > mouseMove();
    > usleep(3000000);
    > x_press_left_button();
    > return 0;
    > }



+ Reply to Thread