I need to listen for mouse events outside of my application, namely:

  • Left click (get click coordinates)
  • Releasing the left mouse button
  • Mouse movement (from the point where the left mouse button was pressed)

Tell me, with what help can this be implemented? Having run through the forums, I was able to find only actions within the application (the program window), but I need to get the values ​​in any program in which the change occurred.

    1 answer 1

    1. If you can only act within the form, then you need to make the form full screen (maximized), but not fullScreen - the contexts are different.
    2. Dale, you need to catch both mouse events and transmit them far - through the form.

    The search led me to an awt solution: https://stackoverflow.com/questions/1190168/pass-mouse-events-to-applications-behind-from-a-java-ui

    Next - the code from the discussion on the link:

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class ClickThrough { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame f = new JFrame("Test"); f.setAlwaysOnTop(true); Component c = new JPanel() { @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.setColor(Color.gray); int w = getWidth(); int h = getHeight(); g2.fillRect(0, 0, w,h); g2.setComposite(AlphaComposite.Clear); g2.fillRect(w/4, h/4, w-2*(w/4), h-2*(h/4)); } }; c.setPreferredSize(new Dimension(300, 300)); f.getContentPane().add(c); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); com.sun.awt.AWTUtilities.setWindowOpaque(f,false); } } 

    And a note from the same place: you need to use the undecorated window (without the status bar, the maximize / minimize buttons, and so on), otherwise it will not work.

    With JavaFX for now I try to repeat the same. It will turn out - I will add the answer.

    • one
      There was a problem: the transparent part of the window does not handle the MouseListener and MouseMotionListener . Only visible (gray form). - user189127
    • I will create an additional question, where we will solve the problem of missing events. - user189127