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.
Asked
Active
Viewed 920 times
0
-
3Java is, fortunately, a particularly poor choice for writing spyware. – Eric Stein Aug 26 '13 at 11:30
-
Do you want to log key events of your own application or globally for whole computer? – AlexR Aug 26 '13 at 11:37
-
its for a program which moves the mouse. I want to log its clicks but if I can make it for whole computer will be better. – user1761818 Aug 26 '13 at 11:46
1 Answers
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
-
-
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