Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find key listener on any where in window

i have develop one java desktop application which monitor the work of the user . now i want to count the number of key press and number of mouse click anywhere in system.

that means when ever my application is running and user typing something in browser or open any folder then mouse click and key press count is increment.

can i use below code

 KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher()

if yes then how it is possible? and if i cannot use that then please say me fast how it is possible. i am beginner in java

like image 639
Uday A. Navapara Avatar asked Apr 09 '14 07:04

Uday A. Navapara


2 Answers

i think this example defiantly help full to you this is the example of the hooks system calls Global Keyboard Listener

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

public class GlobalKeyListenerExample implements NativeKeyListener {
        public void nativeKeyPressed(NativeKeyEvent e) {
                System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

                if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) {
                        GlobalScreen.unregisterNativeHook();
                }
        }

        public void nativeKeyReleased(NativeKeyEvent e) {
                System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
        }

        public void nativeKeyTyped(NativeKeyEvent e) {
                System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
        }

        public static void main(String[] args) {
                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);
                }

                //Construct the example object and initialze native hook.
                GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample());
        }
}
like image 74
Radhey Avatar answered Sep 17 '22 23:09

Radhey


I know it is possible to do it using Windows HOOKS system calls which is easily accesible from C or C++ (Tutorial).

For Java you will need to wrap these system calls. There is a library which already makes this work for you but I have never tested it:

https://code.google.com/p/jnativehook/

like image 32
Pablo Francisco Pérez Hidalgo Avatar answered Sep 19 '22 23:09

Pablo Francisco Pérez Hidalgo