java键盘记录器_java键盘记录器

Main.java:

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRootPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.WindowConstants;

class Main {

public static void main(final String[] args) {

// TODO 自动生成的方法存根

final Recorder R = new Recorder();

final Thread thread = new Thread(R);

thread.setDaemon(true);

final BorderLayout BL = new BorderLayout();

final GridLayout GL = new GridLayout();

final JFrame JF = new JFrame("JKeyRecorder Console");

final JButton JB = new JButton("开始记录");

final JButton JB2 = new JButton("停止记录");

final JButton JB3 = new JButton("查看结果");

final JPanel JP = new JPanel();

final JLabel JL = new JLabel("键盘记录文件位置:/var/log/JKeyRecorder.log");

JB2.setEnabled(false);

JF.setLayout(BL);

JP.setLayout(GL);

JP.add(JB);

JP.add(JB2);

JP.add(JB3);

JF.add(JP, BorderLayout.CENTER);

JF.add(JL, BorderLayout.SOUTH);

JF.setUndecorated(true);

JF.setSize(500, 500);

JF.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

JF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JF.setLocationRelativeTo(null);

JF.setVisible(true);

JB.addActionListener(new ActionListener() {

@Override

public void actionPerformed(final ActionEvent arg0) {

// TODO 自动生成的方法存根

JB.setEnabled(false);

JB2.setEnabled(true);

JL.setText(JL.getText() + " (运行中)");

JB3.setEnabled(false);

JF.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

thread.start();

}

});

JB2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(final ActionEvent arg0) {

// TODO 自动生成的方法存根

JB2.setEnabled(false);

JB3.setEnabled(true);

JB.setEnabled(true);

JF.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

thread.interrupt();

JL.setText("键盘记录文件位置:/var/log/JKeyRecorder.log");

}

});

JB3.addActionListener(new ActionListener() {

@Override

public void actionPerformed(final ActionEvent arg0) {

// TODO 自动生成的方法存根

final JFrame JF2 = new JFrame("JKeyRecorder - 记录结果");

final JScrollPane JSP = new JScrollPane();

final JTextArea JTA = new JTextArea();

JSP.setViewportView(JTA);

JF2.setSize(725, 725);

JF2.setLocationByPlatform(true);

JF2.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

JF2.setUndecorated(true);

JF2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

JF2.add(JSP);

JTA.setEditable(false);

try {

final FileReader FR = new FileReader("/var/log/JKeyRecorder.log");

final BufferedReader BR = new BufferedReader(FR);

String s;

while ((s = BR.readLine()) != null) {

JTA.append(s);

}

FR.close();

BR.close();

} catch (final IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

JF2.setVisible(true);

}

});

}

}

Recorder.java:

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

class Recorder implements Runnable {

@Override

public void run() {

// TODO 自动生成的方法存根

final File F = new File("/var/log/JKeyRecorder.log");

if (F.exists() == false) {

try {

Runtime.getRuntime().exec("touch " + F.getAbsolutePath());

} catch (final IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

new KeyListener() {

@Override

public void keyPressed(final KeyEvent arg0) {

// TODO 自动生成的方法存根

final int keycode = arg0.getKeyCode();

final String keytext = KeyEvent.getKeyText(keycode);

try {

final FileWriter FW = new FileWriter(F);

FW.append(keytext);

} catch (final IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

@Override

public void keyReleased(final KeyEvent arg0) {

// TODO 自动生成的方法存根

}

@Override

public void keyTyped(final KeyEvent arg0) {

// TODO 自动生成的方法存根

}

};

}

}

我是分成了两个*.java文件来写的。

但是它好像并不能记录来自键盘的输入。

我查了一下,貌似KeyListener只适用于GUI?

如果真是这样的话,我要做个在后台运行的即时键盘记录器,该怎么办呢?

如果只用KeyEvent的话,则记录的全都是数字键码,而不是键盘按键上面标的字符。

root@debian:~# cat /var/log/JKeyRecorder.log

root@debian:~#

JKeyRecorder.log是空的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值