Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-25-2007, 02:22 PM
|
#1
|
Member
Registered: Aug 2007
Location: Hungary
Distribution: Fedora, CentOS
Posts: 91
Rep:
|
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,
|
|
|
10-25-2007, 02:59 PM
|
#2
|
Member
Registered: Aug 2007
Location: Hungary
Distribution: Fedora, CentOS
Posts: 91
Original Poster
Rep:
|
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);
}
|
|
1 members found this post helpful.
|
03-19-2009, 10:52 AM
|
#3
|
LQ Newbie
Registered: Mar 2009
Posts: 3
Rep:
|
Quote:
Originally Posted by Sheridan
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
|
|
|
03-19-2009, 11:35 AM
|
#4
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
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.
|
|
|
02-07-2011, 11:42 AM
|
#5
|
LQ Newbie
Registered: Feb 2008
Posts: 2
Rep:
|
Quote:
Originally Posted by Sheridan
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
|
|
|
01-09-2012, 11:04 AM
|
#6
|
LQ Newbie
Registered: Jan 2012
Posts: 2
Rep:
|
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.
Last edited by aman_madaan; 01-09-2012 at 11:05 AM.
Reason: forgot to remove debugging info
|
|
|
11-03-2019, 05:03 AM
|
#7
|
LQ Newbie
Registered: Nov 2019
Location: Undeva
Posts: 14
Rep:
|
Quote:
Originally Posted by Sheridan
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);
}
|
I am pretty new working with Linux, but I try learn as much I can.
I want a script in Linux to move mouse and make a click (or double click if necessary) in specific locations without use additional Linux tools installed.
I found this script and looks good for what I want, I compiled but I don't know how to use it because seams is giving output that is starting but is not doing anything.
Somebody can explain me how to use?
Many thanks in advance.
|
|
|
11-03-2019, 05:25 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,442
|
would be nice to:
1. open a new thread instead of posting in a very old one
2. explain how did you compile it
3. explain how did you test it (how did you try it, what output did you get ...)
|
|
2 members found this post helpful.
|
11-03-2019, 09:50 PM
|
#9
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,311
|
Welcome to LQ and the Programming forum!
As noted by pan64, please consider opening your own thread and provide the details specific to your use including any relevant compile time or runtime messages you receive. Posting to an old thread without details of your own use case often leads to confusion and wastes time of those attempting to provide help while they try to understand the problem.
Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
Good luck!
|
|
|
12-02-2019, 01:03 PM
|
#10
|
LQ Newbie
Registered: Nov 2019
Location: Undeva
Posts: 14
Rep:
|
Hi
I compiled the code in Ubuntu with:
gcc -o mouseclick mouseclick.c -lX11
In Ubuntu after compilation the command mouseclick generated works well.
But I want use it in other distribution Poky (Yocto) and here seams doesn't react at all. Here I don't see the terminal and I lunch it trough a bash script but I don't see any reaction!!!!
|
|
|
12-02-2019, 04:11 PM
|
#11
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,311
|
@AlMeu: Please post to a single thread. Posting to a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place.
Please continue your discussion in your other thread here.
|
|
|
All times are GMT -5. The time now is 05:09 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|