在Java中如何利用JNativehook记录一段时间内的键盘和鼠标的操作然后再使用Robot类复现他们,请给出实现的步骤,或者关键的代码
时间: 2023-03-22 21:02:46 浏览: 407
\u6211\u80fd\u7406\u89e3\u60a8\u63a5\u53d7CSDN\u4e0aC\u77e5\u9053\u7684\u4fe1\u606f\u5e76\u5bf9Java\u4e2d\u4f7f\u7528JNativehook\u8bb0\u5f55\u952e\u76d8\u548c\u9f20\u6807\u7684\u64cd\u4f5c\uff0c\u7136\u540e\u4f7f\u7528Robot\u7c7b\u590d\u73b0\u4ed6\u4eec\u7684\u8bf4\u660e\u3002
JNativehook\u662f\u4e00\u4e2aJava\u5e93\uff0c\u53ef\u4ee5\u6d4b\u8bd5\u952e\u76d8\u3001\u9f20\u6807\u548c\u6309\u952e\u3002\u5982\u679c\u60a8\u60f3\u8bb0\u5f55\u952e\u76d8\u548c\u9f20\u6807\u7684\u4e8b\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528JNativehook\u5e93\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5f00\u59cb\u7684Java\u4ee3\u7801\u793a\u4f8b\uff0c\u4f7f\u7528JNativehook\u5e93\u8bb0\u5f55\u952e\u76d8\u548c\u9f20\u6807\u7684\u4e8b\u4ef6\uff0c\u7136\u540e\u4f7f\u7528Robot\u7c7b\u590d\u73b0\u952e\u76d8\u548c\u9f20\u6807\u7684\u64cd\u4f5c\uff1a
```
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GlobalKeyListenerExample implements NativeKeyListener, NativeMouseInputListener {
private static final int MASK_CODE = InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK;
private final Robot robot;
public GlobalKeyListenerExample() throws AWTException {
this.robot = new Robot();
}
public static void main(String[] args) {
try {
// Add native hook listener
GlobalScreen.registerNativeHook();
// Construct the example object and initialze native hook.
GlobalKeyListenerExample example = new GlobalKeyListenerExample();
GlobalScreen.addNativeKeyListener(example);
GlobalScreen.addNativeMouseListener(example);
GlobalScreen.addNativeMouseMotionListener(example);
GlobalScreen.addNativeMouseWheelListener(example);
// Disable loggers from the example.
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
// Turn off the parent handlers.
logger.setUseParentHandlers(false);
} catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
阅读全文
相关推荐


















