Detecting user activity


#1

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?


#2

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?


#3

Ah - found it - Desktop::getMousePosition might work


#4

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


#5

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


#6

Is there anything similar for detecting keypresses though?


#7

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


#8

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…