说明
Java全局监听鼠标点击,不管是在窗体内点击还是在窗体外点击,都能监听到。
效果图
GlobalMouseListener.java
package cxzgwing;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;
import org.jnativehook.mouse.NativeMouseListener;
import org.jnativehook.mouse.NativeMouseMotionListener;
/**
* https://github.com/kwhat/jnativehook
*/
public class GlobalMouseListener extends JFrame implements NativeMouseInputListener {
private final JTextArea info;
public GlobalMouseListener() {
info = new JTextArea();
// 关闭日志
Logger.getLogger(GlobalScreen.class.getPackage().getName()).setLevel(Level.OFF);
setTitle("GlobalMouseListener");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
info.setEditable(false);
info.setBackground(new Color(0xFF, 0xFF, 0xFF));
info.setForeground(new Color(0x00, 0x00, 0x00));
info.setText("");
JScrollPane scrollPane = new JScrollPane(info);
scrollPane.setPreferredSize(new Dimension(375, 125));
add(scrollPane, BorderLayout.CENTER);
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
e.printStackTrace();
info.append("Error: " + e.getMessage() + "\n");
}
GlobalScreen.addNativeMouseListener(this);
setVisible(true);
}
/**
* @see NativeMouseListener#nativeMouseClicked(NativeMouseEvent)
*/
public void nativeMouseClicked(NativeMouseEvent e) {
appendDisplay(e.paramString());
}
/**
* @see NativeMouseListener#nativeMousePressed(NativeMouseEvent)
*/
public void nativeMousePressed(NativeMouseEvent e) {
appendDisplay(e.paramString());
}
/**
* @see NativeMouseListener#nativeMouseReleased(NativeMouseEvent)
*/
public void nativeMouseReleased(NativeMouseEvent e) {
appendDisplay(e.paramString());
}
/**
* @see NativeMouseMotionListener#nativeMouseMoved(NativeMouseEvent)
*/
public void nativeMouseMoved(NativeMouseEvent e) {
appendDisplay(e.paramString());
}
/**
* @see NativeMouseMotionListener#nativeMouseDragged(NativeMouseEvent)
*/
public void nativeMouseDragged(NativeMouseEvent e) {
appendDisplay(e.paramString());
}
private void appendDisplay(final String output) {
info.append(output + "\n");
try {
if (info.getLineCount() > 100) {
info.replaceRange("", 0, info.getLineEndOffset(info.getLineCount() - 1 - 100));
}
info.setCaretPosition(info.getLineStartOffset(info.getLineCount() - 1));
} catch (BadLocationException ex) {
info.setCaretPosition(info.getDocument().getLength());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(GlobalMouseListener::new);
}
}
maven
<!-- https://mvnrepository.com/artifact/com.1stleg/jnativehook -->
<dependency>
<groupId>com.1stleg</groupId>
<artifactId>jnativehook</artifactId>
<version>2.1.0</version>
</dependency>
技术点
参考链接
[1] kwhat.JNativeHook: Global keyboard and mouse listeners for Java..2021-03-24 12:29
https://github.com/kwhat/jnativehook
[2] codeday.java-如何从JNativeHook库中删除日志记录数据.2019-10-29
http://www.cocoachina.com/articles/99009