Click here to Skip to main content
15,867,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
0 down vote
favorite
I have an Parental Control Application that needs to log all search inputs from all type of browser in operating system.

I have used jnativehook and listening to nativeKeyPressed. It work perfect and as per requirements when running from eclipse or from console as jar executable.

I have used Apache Common Daemon to register this application as windows service.

Now its installed and keep running in task manager but key press event is not firing and log is empty.

I need to know that is it possible for jnativehook to work as per my expectation? if yes then how?

Below is sample application code which is running perfectly.

NOTE: i am registering nativehook two times in order to confirm my self that it works

What I have tried:

Java
package loger;


import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;


public class Main implements NativeKeyListener {
	
	private static Main 
    serviceInstance = new Main();
	
	
	public void nativeKeyPressed(NativeKeyEvent e){
		System.out.println(NativeKeyEvent.getKeyText(e.getKeyCode()).toString());
		
		 String preceedingPath = System.getenv("APPDATA");
		 File dest = new File(preceedingPath +File.separatorChar + "tracer"+File.separatorChar+"");
		 File file = null;
		 if(!dest.exists())
		 {
	    	dest.mkdir();
	    	//file = new File(dest.toString() +File.separatorChar + "Logs.txt");
		 }
		 //else{
			file = new File(dest.toString() +File.separatorChar + "Logs.txt");
		//}
			
		 if(!file.exists())
		 {
			try
			{
				file.createNewFile();
			}
			catch(Exception ex)
			{
			
			}
				
		 }
		try
		{
			FileWriter write = new FileWriter(file , true);
			PrintWriter printLine = new PrintWriter(write);
			printLine.printf("%s"+"%n", NativeKeyEvent.getKeyText(e.getKeyCode()).toString());
			printLine.close();
		}
		catch(Exception ex)
		{
				
		}
		
		if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
            try {
            		GlobalScreen.unregisterNativeHook();
            	} catch (NativeHookException e1) {
            			// TODO Auto-generated catch block
            			e1.printStackTrace();
            		}
        }
	}
	
	public void nativeKeyReleased(NativeKeyEvent e) {
        //System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }
 
    public void nativeKeyTyped(NativeKeyEvent e) {
    	
    }
    
    public static void main(String[] args){
    	
    	String cmd = "start";
        if(args.length > 0) {
           cmd = args[0];
        }
  	
        if("start".equals(cmd)) {        	
        	try {
                GlobalScreen.registerNativeHook();
            }
            catch (NativeHookException ex) {
                System.err.println("There was a problem registering the native hook.");
                System.err.println(ex.getMessage());

                System.exit(1);
            }

            GlobalScreen.addNativeKeyListener(new Main());
            
            System.gc();
           serviceInstance.start();
        }
        else {
           serviceInstance.stop();
        }
    }

    
    /**
     * Flag to know if this service
     * instance has been stopped.
     */
    private boolean stopped = false;
 	
 	
    /**
     * Start this service instance
     */
    public void start() {
 	
       stopped = false;
 		
       System.out.println("My Service Started "
                          + new java.util.Date());
       
       String preceedingPath = System.getenv("APPDATA"); 
       File dest = new File(preceedingPath +File.separatorChar + "tracer"+ File.separatorChar+"");
       
	   System.out.println("File path == "+dest.toString());
	   
       
       
       while(!stopped) {
    	   
    	   try {
               GlobalScreen.registerNativeHook();
           }
           catch (NativeHookException ex) {
               System.err.println("There was a problem registering the native hook.");
               System.err.println(ex.getMessage());

               System.exit(1);
           }

           GlobalScreen.addNativeKeyListener(new Main());
          
           synchronized(this) {
              try {
                 this.wait(10000);  // wait 10 sec minute
              }
              catch(InterruptedException ie){}
           }
        }
       
       
       System.out.println("My Service Finished "
                           + new java.util.Date());
    }
 	
    /**
     * Stop this service instance
     */
    public void stop() {
       stopped = true;
       synchronized(this) {
	      try {
	            GlobalScreen.unregisterNativeHook();
	      } catch (NativeHookException e1) {
	            // TODO Auto-generated catch block
	            e1.printStackTrace();
	            System.out.println(e1.getMessage());
	      }
          this.notify();
       }
    }
    
}
Posted
Updated 3-Aug-17 21:06pm

1 solution

Windows services do not run in a session which has access to a terminal (desktop). That is to say no screen or keyboard (logical or physical) is attached. So bottom line is you cannot do this from a service. And, to be honest, that is just as well as such a feature would open a huge security hole in Windows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900