How to move the mouse cursor from user code?

2019-04-10 20:20发布

问题:

My data comes from an arduino (which gets it from a sensor).
I'd like to have the data processed by an user program (after reading it from /dev/ttyUSB0 ).
After that I need to control the mouse cursor using the output of the program.
(I'd really like to avoid writing a kernel driver at this moment.)

What is the recommended way to do this(on a Linux environment)?
Perhaps a library on top of X...or some tool/script I can directly pipe the data into?

回答1:

Taken from dzone:

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

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

void mouseMove(int x, int y)
{
    Display *displayMain = XOpenDisplay(NULL);

    if(displayMain == NULL)
    {
        fprintf(stderr, "Errore nell'apertura del Display !!!\n");
        exit(EXIT_FAILURE);
    }

    XWarpPointer(displayMain, None, None, 0, 0, 0, 0, x, y);

    XCloseDisplay(displayMain);
}


回答2:

There are a few options I know of:

  1. xte is a command line tool: http://linux.die.net/man/1/xte
  2. if you can use python, xaut might be more to your liking: http://xautomation.sourceforge.net/index.html


回答3:

Or with node-x11:

var x = 100; 
var y = 200;
require('x11').createClient(function(err, display) {
    display.client.WarpPointer(0, display.screen[0].root, 0, 0, 0, 0, x, y);
});