java键盘记录器_java键盘记录器
发布日期:2021-06-24 16:28:42 浏览次数:2 分类:技术文章

本文共 4071 字,大约阅读时间需要 13 分钟。

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是空的。

转载地址:https://blog.csdn.net/weixin_33826897/article/details/114115275 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iosalipay_【报Bug】ios支付宝支付调起支付闪退
下一篇:计算器java小程序_Java写的一个计算器模拟小程序

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月25日 23时17分57秒