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
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());
}
}
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With