Question

I am making a sort of Macro recorder/player

I have done the player part with utils such as java.awt.Robot() which emulates basic human mouse/keyboard output commands, reading an XML file.

I am stuck at the part where I have to record that XML file. I have no idea of which Class I can use to do the opposite of Robot() If yo have any FemaleRobot() for me I would be very happy :D

The only thing in this direction I have so far is :

   while (true) {
        Point pos = MouseInfo.getPointerInfo().getLocation();
        System.out.println(pos.x+" x "+pos.y);
   }

which is not much and not really what I want ^_^, I don't know how to use a Mouse/KeyListener since it would require a Component. If it is the only way, what Compoment do I use as I don't want any graphical java implementation? Should I create a phony Component anyway? Which one?

e.g. I want my recorder to write in the XML how I click on my ubuntu desktop or press enter on firefox.

I guess it's clear, if not I will be checking the answers a lot. Have a nice day and thanks for reading this.

Was it helpful?

Solution

Try jnativehook lib: http://code.google.com/p/jnativehook/wiki/examples

It is very easy to use and may meet your needs.

OTHER TIPS

I regret to inform you it is completely impossible to monitor mouse clicks and keystrokes outside of your form. Java events simply do not fire outside the scope of your form.

The reason java behaves this way is to eliminate the possibility of java based malware attempting to steal sensitive data.

You can find examples of mouse listeners here: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

to Write to a file you can do something like:

FileWriter fileStream = new FileWriter("myfile.extention");
BufferedWriter out = new BufferedWriter(fileStream);
out.write("pos.x+" x "+pos.y");

If you are doing this:

while (true) 
{
    Point pos = MouseInfo.getPointerInfo().getLocation();
    System.out.println(pos.x+" x "+pos.y);
}

You will want to add a Thread.Sleep call in the loop.

Then you can read the file back by doing:

FileInputStream fileStream = new FileInputStream("myfile.extention");
  DataInputStream in = new DataInputStream(fileStream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in))
  String position = br.readLine()

Then you can parse that string to get the values.

So you might do something like:

FileWriter filewrite = new FileWriter("myfile.txt");
BufferedWriter out = new BufferedWriter(filewrite);

while (Recording) 
{
    Point pos = MouseInfo.getPointerInfo().getLocation();
    out.write(pos.x + " " + pos.y);

    Thread.sleep(50);
}

FileInputStream fileStream = new FileInputStream("myfile.txt");
DataInputStream in = new DataInputStream(fileStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in))

String position;
while(position = br.readLine() != null)
{
    String[] positions = position.split(" ");
    int x = Integer.parseInt(positions[0]);
    int y = Integer.parseInt(positions[1]);
}

You will have to use the mouse events to write click positions to the file.

You can first get the resolution of screen.Second thing make a frame that is a size of your screen and make its transparency to 0% .When you click to a position on a screen put down your jframe and use mouse robot to click a position.You can get the coordinates of the entire screen.This way you can record the mouse clicks,But the problem comes is when you need to type in the text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top