package newpackage;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.chart.PieChart.Data;
import javax.swing.JFrame;
import javax.swing.JPanel;
//窗体类
public class MyFrame extends JFrame {
/**
* @param args
*/
JPanel jPanel;
char charA;
public MyFrame(){
this.setSize(500,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Keylogger");
this.setVisible(true);
this.addKeyListener(new MyKeyListener());
jPanel = new JPanel();
add(jPanel);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
char charA = e.getKeyChar();
String s = null;
try {
s = "You pressed "+charA+" at "+getTime();
} catch (ParseException ex) {
Logger.getLogger(MyKeyListener.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(s);
}
private Date getTime() throws ParseException {
Date d = new Date();
String s = null;
s = DateFormat.getDateInstance().format(d);
s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
s = sdf.format(d);
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
s = sdf2.format(d);
DateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");
s = sdf3.format(d);
s = sdf.format(d);
Date today = sdf.parse(s);
Calendar c = Calendar.getInstance();
return c.getTime();
}
}
如果想新建一个类来调用
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
char charA = e.getKeyChar();
String s = null;
try {
s = "You pressed "+charA+" at "+getTime();
} catch (ParseException ex) {
Logger.getLogger(MyKeyListener.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(s);
}
这里的s,但是不知道该怎么编写
下面是我新建的类:
package newpackage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class FileWriter {
public static void main(String[] args) throws IOException
{
FileOutputStream f = new FileOutputStream(new File("D:\\temp\\text.txt"));
PrintStream p = new PrintStream(f);
MyKeyListener m = new MyKeyListener();
p.println(s);
p.close();
}
希望各位大神能给我一些帮助,感激不尽