0

Hi I want to build on java mouse click logger. I need to print the coordinates of mouse clicks in a file. Can you tell me how I can make this. Which APIs to use some examples or links. I need to get all the mouse clicks not only in one window.

user1761818
  • 365
  • 1
  • 7
  • 14

1 Answers1

0

you could implement the MouseListener interface and overwrite the mouseClicked function:

public class MyMouseListener implements MouseListener {

   // [...]

   @Override
   public void mouseClicked(MouseEvent e) {
       System.out.println(e.getLocationOnScreen());
   }

   // [...]
}

Adapt that example to your needs. THe getLocationOnScreen Method returns a "Point" object which you can ask for x/y coordinates.

Ulathar
  • 309
  • 3
  • 13
  • and from where to get the MouseEvent? – user1761818 Aug 26 '13 at 12:07
  • ah ok. you did not specify what kind of programm you are using (for example if you have a Swing gui or a console app or something like that). For an application, that can react on ALL Mouse Inputs without the need of registering listener to GUI components you could take a look at the "java.awt.MouseInfo" package in the Java API. this should provide all the functions you are asking for. For a very simple code example for this purpose can be found here: http://www.javaprogrammingforums.com/java-programming-tutorials/390-how-get-mouse-position-even-if-not-within-our-application.html – Ulathar Aug 26 '13 at 12:19
  • yes thank you but with this class I can't see the location when the mouse is clicked – user1761818 Aug 26 '13 at 14:36
  • @Ulathar Note that 1) `MouseListener` only applies to (can only be added to) Java objects in that JVM. 2) The `MouseInfo` class will *not* provide information on mouse clicks, just location on screen. This answer is noise.. – Andrew Thompson Aug 27 '13 at 01:54
  • so there is now way to get location of mouse click outside of program's window ? – user1761818 Aug 27 '13 at 17:51