Detecting user activity

I’d like to be able to detect when the user has moved away from the computer, and perhaps has left this application running in the foreground. Is there an easy way of detecting whether the user has pressed a key or moved the mouse without putting a load of hooks in everywhere? E.g. iterating through possible MouseInputSources or something?

I guess I could attach a mouseListener with a ‘wants events for child components’ to the top level component? Do I need to do that for each top-level window though if I have multiple windows open?

Ah - found it - Desktop::getMousePosition might work

The desktop class also has a global mouse listener that you could use for this.

1 Like

On it. I presume that only gets called when the app is in the foreground too, which is handy !

Is there anything similar for detecting keypresses though?

No… don’t think we ever added a global key listener.

Yeah - couldn’t see one. People who don’t ever move the mouse are going to be stuffed … I might not win any accessibility points with that one. Maybe I’ll put some other triggers in to reset the activity timeout somewhere…