Re simulate mouse buttons using Xlib

Document Sample
Re simulate mouse buttons using Xlib Powered By Docstoc
					                                   Re: simulate mouse buttons using Xlib

Re: simulate mouse buttons using Xlib

Source: http://newsgroups.derkeiler.com/Archive/Comp/comp.windows.x/2007−03/msg00012.html



      • From: andreyfilkov@xxxxxxxxx
      • Date: 7 Mar 2007 00:58:22 −0800

this code work

// g++ test.cpp −L/usr/X11R6/lib −lX11 −lXtst
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include <X11/extensions/XTest.h>

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?



Re: simulate mouse buttons using Xlib                                                       1
                                Re: simulate mouse buttons using Xlib
       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 <stdio.h>
       #include <stdlib.h>
       #include <string.h>
       #include <sys/time.h>
       #include <unistd.h>

       #include <X11/Xlib.h>
       #include <X11/Xutil.h>
       #include <X11/extensions/XTest.h>


       /**********************************************************************
       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;

Re: simulate mouse buttons using Xlib                                             2
                                   Re: simulate mouse buttons using Xlib



       /* 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);

Re: simulate mouse buttons using Xlib                                       3
                                 Re: simulate mouse buttons using Xlib
       }
       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;
       }


.




Re: simulate mouse buttons using Xlib                                    4

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:48
posted:7/7/2011
language:English
pages:4