I have two booleans: leftPressed and rightPressed. I need them to be set true when their respective mouse buttons are pressed and false when they are not. The location of the mouse and whether or not the program has focus should not matter. This program will not have a GUI. Is this even possible?
Asked
Active
Viewed 803 times
1
-
possible duplicate of [how to obtain mouse click coordinates outside my window in Java](http://stackoverflow.com/questions/2419555/how-to-obtain-mouse-click-coordinates-outside-my-window-in-java) – DNA Feb 18 '12 at 23:31
-
This isn't as trivial as you might think and I think probably requires a mouse hook on the OS. This is something that probably requires native (non-Java) code. – Hovercraft Full Of Eels Feb 18 '12 at 23:34
-
You can get the mouse _location_ using `java.awt.MouseInfo` but it doesn't seem to provide button info. – DNA Feb 18 '12 at 23:36
-
@DNA: Your previous answer polls the mouse, but doesn't get the button state information (ah -- as you've mentioned above). Myself, I'd first see if this is absolutely necessary because often there's a better way to solve the overall problem. If not, then I'd look into using JNA or JNI to solve this if absolutely necessary. – Hovercraft Full Of Eels Feb 18 '12 at 23:37
-
Yes, I deleted the answer because it doesn't address the button issue. – DNA Feb 18 '12 at 23:40
1 Answers
0
Definitely possible, very time consuming though. Use JNA to create a global mousehook. You would have to provide implementations for each platform you planned for it to run on, etc.. For example, here's someone who did it with windows.
I was looking for something similar at one point but found a better way to go about it in my code. However, I did run into this library. It may work out for you.