gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:48 26 4
gpt4 key购买 nike

我创建了一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com