I created the MouseMotionDetection class, whose role is to detect that the user moves the mouse anywhere on the screen.
To this end, I created a new JFrame inside my class constructor with a screen size that is invisible, so basically I observe the movement of the mouse around the screen.
But I have a strange error:
In the current code form, as soon as this class is activated, I detect only one mouse movement and nothing else, after that it stops working. But, if I put a line that sets the back focus of the frame to 0f, 0f, 0f, 0f (transparent) in the comments and then activates, the entire screen turns gray and I continue to track all mouse movements the same way I wanted (I just may i see nothing).
I really don't understand why this is happening, have not seen the problems associated with this, nor in this related javadoc, which discusses MouseMotion events.
This is the code:
public class MouseMotionDetection extends JPanel implements MouseMotionListener{ public MouseMotionDetection(Region tableRegion, Observer observer){ addMouseMotionListener(this); setBackground(new Color(0f,0f,0f,0f)); JFrame frame = new JFrame(); frame.setUndecorated(true); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(screenSize); frame.setBackground(new Color(0f,0f,0f,0f)); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setAlwaysOnTop(true); JComponent contentPane = this; contentPane.setOpaque(true); frame.getContentPane().add(contentPane, BorderLayout.CENTER); frame.setVisible(true); } @Override public void mouseDragged(MouseEvent arg0) { } @Override public void mouseMoved(MouseEvent arg0) { System.out.println("mouse movement detected"); }
source share