If your application always has system focus, you can probably do this using a MouseMotionListener and KeyPressListener and a Timer object or similar.
If you want to be able to monitor 'outside' of your application then I'd guess some system-specific calls are required, in which case JNI is your friend....