LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-25-2007, 02:22 PM   #1
Sheridan
Member
 
Registered: Aug 2007
Location: Hungary
Distribution: Fedora, CentOS
Posts: 91

Rep: Reputation: 21
Question 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,
 
Old 10-25-2007, 02:59 PM   #2
Sheridan
Member
 
Registered: Aug 2007
Location: Hungary
Distribution: Fedora, CentOS
Posts: 91

Original Poster
Rep: Reputation: 21
Lightbulb

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.
Old 03-19-2009, 10:52 AM   #3
mustafamadni
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
Smile

Quote:
Originally Posted by Sheridan View Post
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
 
Old 03-19-2009, 11:35 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
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.
 
Old 02-07-2011, 11:42 AM   #5
Pioz
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by Sheridan View Post
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
 
Old 01-09-2012, 11:04 AM   #6
aman_madaan
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
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
 
Old 11-03-2019, 05:03 AM   #7
AlMeu
LQ Newbie
 
Registered: Nov 2019
Location: Undeva
Posts: 12

Rep: Reputation: Disabled
Quote:
Originally Posted by Sheridan View Post
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.
 
Old 11-03-2019, 05:25 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,276

Rep: Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204Reputation: 4204
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.
Old 11-03-2019, 09:50 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,270
Blog Entries: 11

Rep: Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247
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!
 
Old 12-02-2019, 01:03 PM   #10
AlMeu
LQ Newbie
 
Registered: Nov 2019
Location: Undeva
Posts: 12

Rep: Reputation: Disabled
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!!!!
 
Old 12-02-2019, 04:11 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,270
Blog Entries: 11

Rep: Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247Reputation: 3247
@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.
 
  


Reply

Tags
mouse, x11


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux "Driver"->simulating a mouse nobodynews Linux - Newbie 6 11-01-2007 05:13 PM
KDE Menu by mouse click Shafted Linux - Software 2 04-02-2007 04:31 AM
Wireless mouse/kboard combo - require a click everytime for mouse to work godfrank Linux - Hardware 2 10-02-2006 07:26 PM
mouse: single-click becomes double-click kinzlaw Linux - Hardware 2 08-24-2005 08:55 PM
X freezes on mouse click. bruno buys Debian 3 09-23-2004 03:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration