Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control mouse by writing to /dev/input/mice

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.

like image 712
Vishal Avatar asked Dec 15 '13 14:12

Vishal


2 Answers

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

like image 80
Jappie Kerk Avatar answered Sep 21 '22 15:09

Jappie Kerk


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; } 
like image 36
Raydel Miranda Avatar answered Sep 22 '22 15:09

Raydel Miranda