Java – detect mouse movement on the screen
I created a mousemotiondetection class, which just detects whether the user has moved the mouse anywhere on the screen
To this end, I created a new JFrame in my class constructor. Its screen size is invisible, so I basically observe the mouse movement on the whole screen
But I have a strange mistake:
In the current form of the code, once this class is activated, I only detect a mouse movement without anything else, and it will stop working after that However, if I put the line of setting the frame background in 0f, 0f, 0f (transparent) in the comment and activate it, the whole screen turns gray, and I will track all mouse actions as I need (I just can't see anything)
I really don't understand why this happens. I don't see the relevant problems or Javadoc, which discussions 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)); JFrame frame = new JFrame(); frame.setUndecorated(true); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(screenSize); frame.setBackground(new Color(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"); }
Solution
Completely transparent frames do not receive mouse events
This is an alternative to using mouseinfo This applies to the components of the application Is invisible (transparent), unfocused, or minimized
import java.awt.*; import java.awt.event.*; import java.awt.geom.GeneralPath; import java.awt.image.BufferedImage; import javax.swing.*; import javax.swing.border.EmptyBorder; public class MouseMoveOnScreen { Robot robot; JLabel label; GeneralPath gp; Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); MouseMoveOnScreen() throws AWTException { robot = new Robot(); label = new JLabel(); gp = new GeneralPath(); Point p = MouseInfo.getPointerInfo().getLocation(); gp.moveTo(p.x,p.y); drawLatestMouseMovement(); ActionListener al = new ActionListener() { Point lastPoint; @Override public void actionPerformed(ActionEvent e) { Point p = MouseInfo.getPointerInfo().getLocation(); if (!p.equals(lastPoint)) { gp.lineTo(p.x,p.y); drawLatestMouseMovement(); } lastPoint = p; } }; Timer timer = new Timer(40,al); timer.start(); } public void drawLatestMouseMovement() { BufferedImage biOrig = robot.createScreenCapture( new Rectangle(0,d.width,d.height)); BufferedImage small = new BufferedImage( biOrig.getWidth() / 4,biOrig.getHeight() / 4,BufferedImage.TYPE_INT_RGB); Graphics2D g = small.createGraphics(); g.scale(.25,.25); g.drawImage(biOrig,label); g.setstroke(new Basicstroke(8)); g.setColor(Color.RED); g.draw(gp); g.dispose(); label.setIcon(new ImageIcon(small)); } public JComponent getUI() { return label; } public static void main(String[] args) throws Exception { Runnable r = new Runnable() { @Override public void run() { JPanel ui = new JPanel(new BorderLayout(2,2)); ui.setBorder(new EmptyBorder(4,4,4)); try { MouseMoveOnScreen mmos = new MouseMoveOnScreen(); ui.add(mmos.getUI()); } catch (AWTException ex) { ex.printStackTrace(); } JFrame f = new JFrame("Track Mouse On Screen"); // quick hack to end the frame and timer f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(ui); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); } }; SwingUtilities.invokelater(r); } }