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是空的。
java鍵盤記錄器