Link to home
Start Free TrialLog in
Avatar of mgcIT
mgcITFlag for United States of America

asked on

Record mouse position on click

I'd like to create a class that will output (to a text file) the location of the mouse on screen whenever the mouse is clicked.  The data collected will later be used with the Robot class to automate some trivial tasks.  How can this be accomplished?  Please give examples.

Thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You just need to write to the file in the actionPerformed handler for the MouseListener. Open the file in append mode.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As CEHJ stated, you can use the mouse listener or you could use an eventhandler that listens for mouse clicks.  Open the file in append Mode or it will erase data from before.  But that will be a lot of file opening if you write to the file on every mouse click.  I would recommend you add to an ArrayList and then save the locations after each session.
It might be better to cache many different positions before writing to file. Depends on how many there are
Avatar of mgcIT

ASKER

hi, thanks for the replies.  

CEHJ:  how would i associate this with the MouseListener?

Also, in your code I don't see a getPoint() method for the ActionEvent Class.  Where is that method coming from?

Thanks
Sorry, that code line should have read:

public void mouseClicked(MouseEvent e) {
Given a cache called 'points' in the form of a List, you can do something like:

        public void mouseClicked(MouseEvent e) {
                if (points.size() >= LIMIT) {
                        PrintWriter out = null;

                        try {
                                out = new PrintWriter(new FileWriter("mousepos.txt", true));

                                for (int i = 0; i < points.size(); i++) {
                                        out.println(points.get(i));
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                        } finally {
                                out.close();
                        }
                } else {
                        points.add(e.getPoint());
                }
        }
Avatar of mgcIT

ASKER

ok i've gotten it to compile now but no output.  I am just click on the screen (not actually on a JFrame or anything like that).  does that matter?   The java app is just minimized.

I'm not too worried about the cache.  this is only a one-time thing and will be recording only 10-20 mouse clicks.  Again, it's just to help with another app that will utilize the robot class.  I need to know the positions to move the mouse to in order to automate some tasks that don't have a command line option to script.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There's some JNI here that will allow system-wide mouse event capture:

http://forum.java.sun.com/thread.jspa?threadID=632369
oh..i created a mini videogame that followed my mouse without using native code but maybe java has drastically changed...i did that in 2002...
but my program was a desktop app...not a web app.
>>oh..i created a mini videogame that followed my mouse without using native code

That would have been only within the application window - not within *any* window
gotcha, CEHJ.  i guess it would be complicated to do that.  millions of threads produced everytime a new window is opened...better to do it like you said.
Avatar of mgcIT

ASKER

>> There's some JNI here that will allow system-wide mouse event capture

this sounds good but it is way over my head, and I don't know anything about c++

what if I were to just use a Jframe and maximize it and record any clicks on that Jframe.  Would that work?  It's not exactly what I wanted but could at least give me a general idea of the coordinates i want to use so i'm not just guessing.  because in the app i'm creating i'll need to point the mouse to several locations using

myRobot.mouseMove(x,y)

thanks
you could create a window using jframe and do it that way...listen for mouse events...that's how the game i made worked...
Avatar of mgcIT

ASKER

ok here is what I have so far.  Still not getting any output.  This was created in NetBeans:

import java.awt.event.MouseEvent;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;


public class RecordMouseClicks extends javax.swing.JFrame {
   
    /** Creates new form RecordMouseClicks */
    public RecordMouseClicks() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();

        getContentPane().setLayout(null);

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mouseClicked(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jLabel1.setText("Recording Mouse Clicks");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 10, 200, 40);

        pack();
    }

    private void mouseClicked(java.awt.event.MouseEvent evt) {

        PrintWriter out = null;
      try {      
            out = new PrintWriter(new FileWriter("c:\\mousepos.txt", true));
            out.println(evt.getPoint());

      }
      catch(IOException ioe) {
            ioe.printStackTrace();
      }
      finally {
            out.close();
      }
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new RecordMouseClicks().show();
    }

   
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
   
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgcIT

ASKER

perfect... thanks very much for the help!
:-)