LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Simulating a mouse click (http://www.linuxquestions.org/questions/programming-9/simulating-a-mouse-click-594576/)

Sheridan 10-25-2007 02:22 PM

Simulating a mouse click
 
Hi Guys :)

I need to write a very simple, very little application that I will be able to insert into a crontab and it will move the mouse pointer to a give coordinate and simulate a left mouse button click (press and release once) at that very point.

Ive got so far the "moving" part straightened out. This little script moves the pointer where I want it to.

Code:

#include "Xlib.h"
int main() {
    int delta_x = 500, delta_y = 160;
    Display *display = XOpenDisplay(0);
    Window root = DefaultRootWindow(display);
    XWarpPointer(display, None, root, 0, 0, 0, 0, delta_x, delta_y);
    XCloseDisplay(display);
    return 0;
}

However... I have little experience with X programming and my C++ is rusty also. Can you help me complete this code, clean and possibly most "simple", to send a mouse "click" event to where the cursor has been moved as described above?

Believe it or not, this is very important for me, but I have little clue. :(

So... Any help is appreciated... :)

By,

Sheridan 10-25-2007 02:59 PM

Just for the benefit of woever is having the same problem in the future. I found this fine code on another webpage. It works just fine. :)

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

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

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, "Errore nell'invio dell'evento !!!\n");
       
        XFlush(display);
       
        usleep(100000);
       
        event.type = ButtonRelease;
        event.xbutton.state = 0x100;
       
        if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
       
        XFlush(display);
       
        XCloseDisplay(display);
}


mustafamadni 03-19-2009 10:52 AM

Quote:

Originally Posted by Sheridan (Post 2936738)
Just for the benefit of woever is having the same problem in the future. I found this fine code on another webpage. It works just fine. :)

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

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

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, "Errore nell'invio dell'evento !!!\n");
       
        XFlush(display);
       
        usleep(100000);
       
        event.type = ButtonRelease;
        event.xbutton.state = 0x100;
       
        if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Errore nell'invio dell'evento !!!\n");
       
        XFlush(display);
       
        XCloseDisplay(display);
}



Hi,

Will you please guide me how to use the above script.

Thanks

Regards

Mmad

theYinYeti 03-19-2009 11:35 AM

This is an old post and I'm not sure the original poster will see your question. So just in case, here's a link you might find useful:
http://www.semicomplete.com/projects/xdotool/

This tool lets you control mouse and keyboard.

Yves.

Pioz 02-07-2011 11:42 AM

Quote:

Originally Posted by Sheridan (Post 2936692)
Hi Guys :)
I need to write a very simple, very little application that I will be able to insert into a crontab and it will move the mouse pointer to a give coordinate and simulate a left mouse button click (press and release once) at that very point.

Check this code: https://gist.github.com/726474

aman_madaan 01-09-2012 11:04 AM

Code:

//sg
#include <X11/Xlib.h>
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

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

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);
}
int main(int argc,char * argv[]) {
int i=0;
    int x , y;
x=atoi(argv[1]);
y=atoi(argv[2]);
    Display *display = XOpenDisplay(0);
 Window root = DefaultRootWindow(display);

    XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);

mouseClick(Button1);
XFlush(display);


    XCloseDisplay(display);
    return 0;
}

coordinates where click is to be generated are passed as command line args.


All times are GMT -5. The time now is 02:16 AM.