4

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sinekonata
  • 364
  • 3
  • 11

4 Answers4

3

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

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

Don Scott
  • 3,179
  • 1
  • 26
  • 40
Jovo Krneta
  • 548
  • 1
  • 14
  • 36
2

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.

Don Scott
  • 3,179
  • 1
  • 26
  • 40
  • 1
    So java's limitation here is forcing me to code this in another language? I work mainly on linux, do you know the language I should favor for this? C#? – sinekonata Jan 06 '12 at 08:30
  • 1
    Well with c# you would need a compatibility layer. I would look into using C++. – Don Scott Jan 07 '12 at 19:56
0

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.

Axis
  • 826
  • 7
  • 22
  • This is not what I want, this code I could have done if what I wanted was only the position of the mouse every 50 millis. What I want is to record my mouse clicks and my keyboard keys as jitbit would. – sinekonata Jan 06 '12 at 02:01
  • Oh I miss understood. I do not believe that Java has the ability to register events that happen outside the application window. If you do not specifically need to use java I would recommend you port your code to C#. The syntax is basically the same and it will give you much more control over input events. – Axis Jan 06 '12 at 02:19
  • I thought so but I don't know nothing of .net and would like to use my app in linux, windows and even macos. – sinekonata Jan 06 '12 at 02:33
  • 1
    .Net will run in Linux and OSX on Mono. There are tons of tutorials here: http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx C# has very similar syntax to Java you should be able to pick it up after a couple of tutorials. – Axis Jan 06 '12 at 02:54
-1

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

Jovo Krneta
  • 548
  • 1
  • 14
  • 36