I want my program to detect if the mouse is pressed down anywhere on the Screen and not just the program window. It should also detect when the mouse is being released. Is it possible in Java to detect this outside of my program window?
Asked
Active
Viewed 1,294 times
1
-
2I'm not sure if it only works in your program but have you tried using a mousehandler – user12346352 Aug 07 '18 at 13:08
-
Yes! It's possible. You need to add a `MouseMotionListener` and listen for `mouseDragged` event – Mohit Aug 07 '18 at 13:32
-
@Mohit your sure that `MouseMotionListener` also supports `MousePressed` and `MouseReleased`? – user12346352 Aug 07 '18 at 13:52
-
Does Mousemotionlistener work outside of the program window? – Aug 07 '18 at 14:02
-
@AllesLegti Yes Mohit already set that the MouseMotionListener can be used outside the program – user12346352 Aug 07 '18 at 14:06
-
@AllesLegit What do you want to achieve? – Mohit Aug 07 '18 at 14:25
-
1*"anywhere on the Screen"* What is the business of your app. knowing what I click elsewhere on the screen? – Andrew Thompson Aug 07 '18 at 14:31
2 Answers
0
java.awt.event.MouseMotionListener is only going to give you information about mouse movement inside your application window. For events that occur outside that window, there is no way around MouseInfo.getPointerInfo. This is a duplicate of Java mouse motion anywhere on screen.

Martin Špelina
- 51
- 4
-
I'm asking for mouse presses.... Look what this guy wrote: You will only receive mouseMoved events, never mouseDragged events. That's because there is no way to receive information about clicks outside the main window. – Aug 07 '18 at 13:45
-
@AllesLegit try to use a `MouseListener` instead you can find it [here](https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html) – user12346352 Aug 07 '18 at 13:53
-
@MartinŠpelina why don't you tell him that before? he wrote in his Question that he Needs a mousepressed and released – user12346352 Aug 07 '18 at 13:56