Java - Guardar ejecución en fichero

 
Vista:
sin imagen de perfil

Guardar ejecución en fichero

Publicado por Guillermo (4 intervenciones) el 24/01/2017 00:32:50
Tengo el siguiente problema:
Tengo un código en java que, mediante la librería JNativeHook, me imprime en pantalla las teclas pulsadas con el teclado. Mi problema llega cuando intento sacar eso a un archivo. Les pongo el código del programa a secas y de mi intento de extraer a fichero (el problema de mi intento es que en el fichero solo me pone la ultima tecla pulsada :S)

CÓDIGO PROGRAMA SIN EXTRAER EN FICHERO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
/*Primero se implementará la interfaz que contiene los métodos necesarios para el
 * manejo de las teclas según su comportamiento
 */
 
public class Main implements NativeKeyListener {
 
	public static void main(String[] args) {
		//Se inicia la rutina de inicialización de los componentes de la librería JNativeHook
		try {
			GlobalScreen.registerNativeHook();
		} catch(Exception e) {
			e.printStackTrace();
		}
 
		/*Es necesario decirle a la instancia de GlobalScreen que se agregará un Listener,
		 * ya que la clase Main implementa la interfaz del Listener sólo se crea una instancia
		 * de dicha clase para colocarla como argumento:
		*/
		GlobalScreen.getInstance().addNativeKeyListener(new Main());
	}
 
	//El método que se utilizará por ahora es nativeKeyPressed:
	@Override
	public void nativeKeyPressed(NativeKeyEvent e) {
		//El código que imprimirá en la salida estándar cuál tecla se ha presionado es:
		System.out.println(NativeKeyEvent.getKeyText(e.getKeyCode()));
	}
 
	@Override
	public void nativeKeyReleased(NativeKeyEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void nativeKeyTyped(NativeKeyEvent e) {
		// TODO Auto-generated method stub
 
	}
}

INTENTO FALLIDO DE EXTRAER EN FICHERO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
/*Primero se implementará la interfaz que contiene los métodos necesarios para el
 * manejo de las teclas según su comportamiento
 */
 
public class Main implements NativeKeyListener {
 
 
 
	public static void main(String[] args) {
 
            escribir();
 
		//Se inicia la rutina de inicialización de los componentes de la librería JNativeHook
		try {
			GlobalScreen.registerNativeHook();
		} catch(Exception e) {
			e.printStackTrace();
		}
 
		/*Es necesario decirle a la instancia de GlobalScreen que se agregará un Listener,
		 * ya que la clase Main implementa la interfaz del Listener sólo se crea una instancia
		 * de dicha clase para colocarla como argumento:
		*/
		GlobalScreen.getInstance().addNativeKeyListener(new Main());
	}
 
        public static void escribir(){
        }
 
	//El método que se utilizará por ahora es nativeKeyPressed:
	@Override
	public void nativeKeyPressed(NativeKeyEvent e) {
 
            FileWriter fichero = null;
            PrintWriter pw = null;
            try{
                fichero = new FileWriter("information.txt");
                pw = new PrintWriter(fichero);
                pw.println(NativeKeyEvent.getKeyText(e.getKeyCode()));
 
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } finally{
                try{
                    if(null != fichero)
                        fichero.close();
                } catch (Exception e2){
                    e2.printStackTrace();
                }
            }
 
	    //El código que imprimirá en la salida estándar cuál tecla se ha presionado es:
 
	}
 
	@Override
	public void nativeKeyReleased(NativeKeyEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void nativeKeyTyped(NativeKeyEvent e) {
		// TODO Auto-generated method stub
 
	}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder