PDA

View Full Version : API to move mouse, grab pixels on screen?



solarwind
March 4th, 2008, 11:11 PM
Hey all, is there a Linux API (preferably C++) to:
* Move the mouse to any coordinate on the screen.
* Get the position of the mouse on the screen.
* Get the colour value of a pixel on the screen at any given coordinate
?

mike_g
March 4th, 2008, 11:28 PM
SDL maybe? I'm not 100% sure about setting the mouse co-ords but it does input so i imagine it should be able to do that.

solarwind
March 4th, 2008, 11:34 PM
SDL maybe? I'm not 100% sure about setting the mouse co-ords but it does input so i imagine it should be able to do that.

So SDL can work directly with X? Let's say I want to make a simple command line C++ program to get the current coordinates of the mouse and RGB values for that coordinate when run. Can SDL do that?

mike_g
March 4th, 2008, 11:37 PM
Nah I don't think so. It will just set up a screen with double buffering to draw to, then you can get pixel values within it. Sorry, dident know that was what you were after.

solarwind
March 4th, 2008, 11:38 PM
Nah I don't think so. It will just set up a screen with double buffering to draw to, then you can get pixel values within it. Sorry, dident know that was what you were after.

It's ok, thanks for the reply anyway!

nanotube
March 5th, 2008, 05:31 AM
well, for python, there's python-xlib (http://python-xlib.sourceforge.net/)

there's just plain xlib for c (http://tronche.com/gui/x/xlib/)

both will be capable of doing what you say you want to do.

fuelnatchos
May 2nd, 2008, 04:30 PM
Here, try this code. It uses python-xlib and does exactly what you want ! (taken from python-linux implementation of wiiwhiteboard: sourceforge.net/project/wiiwhiteboard)

import Xlib.display
import Xlib.ext.xtest

class MouseControl:
def __init__(self):
self.display = Xlib.display.Display()
self.screen = self.display.screen()
self.root = self.screen.root

def mouse_click(self, button):
self.mouse_down(button)
self.mouse_up(button)

def mouse_down(self, button): #button= 1 left, 2 middle, 3 right
Xlib.ext.xtest.fake_input(self.display,Xlib.X.Butt onPress, button)
self.display.sync()

def mouse_up(self, button):
Xlib.ext.xtest.fake_input(self.display,Xlib.X.Butt onRelease, button)
self.display.sync()

def mouse_warp(self, x,y):
self.root.warp_pointer(x,y)
self.display.sync()

def get_screen_resolution(self):
return self.screen['width_in_pixels'], self.screen['height_in_pixels']

Wybiral
May 2nd, 2008, 04:50 PM
...

Posting Python code...

http://icanhascheezburger.files.wordpress.com/2007/04/wrong-mike.jpg

lapubell
May 2nd, 2008, 08:23 PM
hey wybiral, nice to see you again. want to show me the right way?

Mr.Macdonald
May 4th, 2008, 03:03 AM
If you're good with java, then the Robot class is perfect for you.

Java Doc:
java.awt.Robot (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html)

I have used it to do just what you want.


public class Robo {

public static void main(String[] args) throws AWTException {

Robot robot = new Robot();
robot.mouseMove(100, 100);
robot.getPixelColor(100, 100);
}
}

LaRoza
May 4th, 2008, 03:13 AM
hey wybiral, nice to see you again. want to show me the right way?

Use the tags for preserving formating. php and code are used. (There is more in the sticky on how to post)

solarwind
May 4th, 2008, 04:03 AM
If you're good with java, then the Robot class is perfect for you.

Java Doc:
java.awt.Robot (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html)

I have used it to do just what you want.


public class Robo {

public static void main(String[] args) throws AWTException {

Robot robot = new Robot();
robot.mouseMove(100, 100);
robot.getPixelColor(100, 100);
}
}

Java is my best language. Thanks a LOT!

solarwind
May 4th, 2008, 04:24 AM
Hmm, this is very slow in Java though. Is there a way to do it in C++?

SuporteTecnicoID
November 30th, 2008, 02:51 PM
Podem usar o programa:???

movemouse

http://www.indexdata.com.br/Linux/Pacotes/AutoIT/movemouse

atenciosamente...

SuporteTecnicoID
www.indexdata.com.br
;-);-);-)

Aqlor
June 3rd, 2010, 04:50 PM
I know this post is basically dead but there still is a need for this.

Others and me have tried java too but this is really really and I mean really slow.

For example if I want to look for a red pixel on a 100 by 100 pixels square it will take just too much time.

Another thing is that with that xlib example you are just moving the mouse, that is just one of three objectives. Still missing getting the position of the mouse and getting the color value. I am not saying that with xlib it's impossible to do it, I have no clue.

It's weird how a scripting language like AutoIt can do it so much better on windows and on linux is basically impossible to get something similar to work.

SuporteTecnicoID nao me parece boa ideia deixar aqui replies em português e não me parece que movemouse faça mais do que mover o rato.