I am using Ubuntu 12.04. For one of my applications I require to control the mouse in software using a script.
I understand that the mouse device is /dev/input/mice
. If I do a cat /dev/input/mice
and then move my mouse, I see a lot of output being dumped to the screen.
Now I wish to remove the mouse, and have a script which writes to /dev/input/mice
in order to control the mouse pointer
Please help me with commands for the following:
(1) Perform a left click
(2) Perform a right click
(3) Move the mouse from one location to another.
Kindly note that I am looking for a shell script solution, rather than a C/C++ solution.
this is not trough the file you mentioned, but its way quicker to use this tool instead of decypering the dump of that file. And it does everything you want in bash.
xdotool does the trick in my terminal.
this is the package site for ubuntu. you probably can install it trough
# apt-get install xdotool
I could just emerge it on gentoo without adding any repositories.
the tool works fairly simple:
#! /bin/bash # move the mouse x y xdotool mousemove 1800 500 # left click xdotool click 1 # right click xdotool click 3
found it here
If you are brave, and you don't want to depend on any third party tool, you should use Xlib. Documentation can be found here. Also you can try python-xlib if you don't want to mess with C/C++.
Check this thread for an example (C/C++).
This is an example of a program that receives a coord and simulates a mouse click at that position.
#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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With