问题
I'm writing an application that monitors the person’s mouse and keyboard. If they have not used the keyboard or mouse for 1 minute, it will pop up a message that says “You have not used the mouse or keyboard for 1 minute” along with an OK button.
How do I do this in Java?
回答1:
You need a bit of C/C++ code and call SetWindowsHookEx This function allows you to hook into Windows events and receive a copy.
This question contains code to get you started: JNA Keyboard Hook in Windows
回答2:
If you want to do this for only your application, then its very simple. You can simply add listerns i.e Toolkit.getDefaultToolkit.addAwtEventListener(..)
.
But for the system as a whole, I am afraid, it cannot be done in java, you may use JNI though.
回答3:
If you only want to monitor activity in Java application windows, it's trivial – all you have to do is register to the appropriate event.
But to monitor all mouse and keyboard activity in the OS you will have to hook to the API which is platform dependent and will require using the JNI
来源:https://stackoverflow.com/questions/1714349/global-keylogger-in-java