有没有办法让Java应用程序检测屏幕是否被锁定?

时间:2021-07-24 06:33:23

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.

我正在寻找一种方法来了解Java应用程序(没有JNI - 它是一个多平台应用程序)可以检测屏幕是否被锁定。

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.

有关信息...我们的应用程序在用户工作时记录时间 - 我们希望它在屏幕被锁定时自动停止录制,而不是用户必须明确地停止录制。

2 个解决方案

#1


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...

我不认为有这样的API,甚至没有办法做到这一切。解析java.awt.Robot生成的屏幕截图既不是平台也不是版本甚至是配置独立的,一般来说,我不认为“屏幕被锁定”是一个足够明确的概念,在这个意义上使用 - 在Linux上,可以有不止一个“屏幕”(X服务器),你可以切换到控制台终端,你可以让运行一台(或多台)机器的应用程序通过网络在另一台机器上显示它们的GUI ...

#2


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

我确认似乎没有Java API来检测屏幕锁定状态。

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).

Spark开发人员在2006年试了一下(就像这个帖子所示),但没有透露具体实现的任何细节。但是,它可能涉及JNI和WTSQuerySessionInformation等函数的本机调用(要检测是否断开连接使用WTSQuerySessionInformation(NULL,WTS_CURRENT_SESSION,WTSConnectState)并查找WTSDisconnected)。

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

因此,正如迈克尔指出的那样,据我所知,没有“多平台”的普遍答案。

#1


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...

我不认为有这样的API,甚至没有办法做到这一切。解析java.awt.Robot生成的屏幕截图既不是平台也不是版本甚至是配置独立的,一般来说,我不认为“屏幕被锁定”是一个足够明确的概念,在这个意义上使用 - 在Linux上,可以有不止一个“屏幕”(X服务器),你可以切换到控制台终端,你可以让运行一台(或多台)机器的应用程序通过网络在另一台机器上显示它们的GUI ...

#2


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

我确认似乎没有Java API来检测屏幕锁定状态。

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).

Spark开发人员在2006年试了一下(就像这个帖子所示),但没有透露具体实现的任何细节。但是,它可能涉及JNI和WTSQuerySessionInformation等函数的本机调用(要检测是否断开连接使用WTSQuerySessionInformation(NULL,WTS_CURRENT_SESSION,WTSConnectState)并查找WTSDisconnected)。

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

因此,正如迈克尔指出的那样,据我所知,没有“多平台”的普遍答案。