简体   繁体   English

如何在OS X中检测屏幕是否关闭?

[英]How to detect whether the screen is off in OS X?

I'm making an application that should only try to run if the display is on (among other things), but I couldn't find a way to check via commandline. 我正在制作一个仅在显示器打开(除其他功能外)时才尝试运行的应用程序,但我找不到通过命令行进行检查的方法。 Checking how long the user has been idle and their preferred idle time via pmset -g doesn't always work either, because of various programs that can change this. 通过pmset -g检查用户空闲了多长时间以及他们的首选空闲时间也不总是可行,因为有各种程序可以更改此设置。

Is there a way (via commandline, or in Java) to check whether the screen is currently off in OS X? 有没有办法(通过命令行或Java)检查OS X中当前屏幕是否关闭?

This bit of C will detect if the screens are asleep or awake: C这一位将检测屏幕是否处于睡眠状态或唤醒状态:

CGDirectDisplayID ids[20];
uint32_t num_ids;
if (kCGErrorSuccess != CGGetActiveDisplayList(20, ids, &num_ids)) {
    printf("Oops\n");
    return 1;
}
boolean_t asleep = true;
for (int i = 0; i < num_ids; i++) {
    asleep &= CGDisplayIsAsleep(ids[i]);
}
if (asleep) {
    printf("Asleep\n");
    return 1;
} else {
    printf("Awake\n");
    return 0;
}

This can be horridly translated to some JNA using: 可以使用以下方法将其可怕地转换为某些JNA

CoreGraphics.java : CoreGraphics.java

import com.sun.jna.*;
import com.sun.jna.ptr.IntByReference;

public interface CoreGraphics extends Library {
    CoreGraphics INSTANCE = (CoreGraphics)Native.loadLibrary("CoreGraphics", CoreGraphics.class);

    class int32_t extends IntegerType {
        public static final int SIZE = 4;
        public int32_t() {
            this(0);
        }
        public int32_t(long value) {
            super(SIZE, value, false);
        }
    };

    public static class CGError extends int32_t {
        public static final CGError Success = new CGError(0);

        public CGError() { this(0); }
        public CGError(int value) { super(value); }
    };

    public static class CGDirectDisplayID extends int32_t {
    };

    CGError CGGetActiveDisplayList(int maxDisplays, CGDirectDisplayID[] activeDisplays, IntByReference displayCount);
    boolean CGDisplayIsAsleep(CGDirectDisplayID disp);
}

Example program making use of the interface - jna_monitors_run.java : 使用接口jna_monitors_run.java示例程序:

import com.sun.jna.*;
import com.sun.jna.ptr.IntByReference;

public class jna_monitors_run {

    static final int MAX_DISPLAYS = 20;
    public static void main(String[] args) {
        IntByReference ib = new IntByReference();
        CoreGraphics.CGDirectDisplayID ids[] = new CoreGraphics.CGDirectDisplayID[MAX_DISPLAYS];
        if (! CoreGraphics.CGError.Success.equals(CoreGraphics.INSTANCE.CGGetActiveDisplayList(
                MAX_DISPLAYS, ids, ib))) {
            System.exit(2);
        }
        boolean is_asleep = true;
        int i = ib.getValue();
        while (--i >= 0) {
            is_asleep &= CoreGraphics.INSTANCE.CGDisplayIsAsleep(ids[i]);
        }
        System.out.println(is_asleep);
        System.exit(is_asleep ? 1 : 0);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM