java - 检测屏幕上的鼠标移动

标签 java swing user-interface mouseevent translucency

我创建了一个 MouseMotionDetection 类,它的作用只是检测用户是否将鼠标移动到屏幕上的任何位置。

为此,我在我的类构造函数中创建了一个新的 JFrame,其屏幕尺寸是不可见的,所以基本上我在整个屏幕上观察鼠标运动。

但是,我有一个奇怪的错误:

在代码的当前形式中,一旦这个类被激活,我只检测到一个鼠标 Action ,没有别的,它在那之后立即停止工作。但是,如果我把将帧背景设置为 0f、0f、0f、0f(透明)的行放在评论中然后激活,整个屏幕就会变成灰色,我会按照我的需要继续跟踪所有鼠标 Action (我可以什么都看不到)。

我真的不明白为什么会这样,周围没有看到相关问题,也没有在这个相关javadoc, which discusses MouseMotion events.

这是代码:

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");
}

最佳答案

完全透明的框架不接收鼠标事件。

这是使用 MouseInfo 的替代方法。这适用于应用程序的组件。不可见(透明)、未聚焦或最小化。

enter image description here

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, 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, 0, 0, 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, 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);
    }
}

关于java - 检测屏幕上的鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467866/

相关文章:

java - 使用volly进行网络连接并解析JSON

java - 创建类似 JToolTip 的组件

Java SWT控件修改后不改变

ios - 如何使用 Swift 3 创建标尺/刻度 slider ?

java - 我试图在java中绘制正方形,但它不起作用

android - 将自定义字体设置为对话框

Java 复数,3 类

java - 在应用程序插件中调整 Gradle startScripts

java - SerialVersionUID 未在 Servlet 中声明

java jpanel如何优化绘画