Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for a Java application to detect if the screen is locked?

Tags:

java

I'm looking for a way to know in a Java application (without JNI - it's a multi-platform application) can detect if the screen is locked.

For information... our application records time while a user is working - we want it to automatically stop recording when the screen is locked instead of the user having to do so explicitly.

like image 900
Nick Pierpoint Avatar asked Jan 16 '09 13:01

Nick Pierpoint


2 Answers

I don't think there is an API for that or even a way to do it all. Parsing screenshots generated by java.awt.Robot is neither platform- nor version- or even configuration-independant, and in general, I don't think "screen is locked" is a well-enough defined concept to be used in this sense - on Linux, there can be more than just one "screen" (X server), you can switch to console terminals, you can have applications running one (or more) machines display their GUIs on another machine over the network...

like image 85
Michael Borgwardt Avatar answered Sep 19 '22 16:09

Michael Borgwardt


I confirm there seems to be no Java API to detect a screen lock status.

Spark developer gave it a try in 2006 (like this thread shows), but without giving out any details on the specific of the implementation.
However, it is likely it involved JNI and native call to functions like WTSQuerySessionInformation (To detect if disconnected use WTSQuerySessionInformation(NULL, WTS_CURRENT_SESSION, WTSConnectState) and look for WTSDisconnected).

So, as Michael pointed out, there is no "multi-platform" universal answer (to the best of my knowledge).

like image 45
VonC Avatar answered Sep 20 '22 16:09

VonC