在 Java 中使用 JNA 关闭监视器未按预期工作
Turning off monitor with JNA in Java doesn't work as expected
使用this example of offing monitor with JNA 我遇到了以下问题:
显示器按预期关闭,但会在一秒钟内立即打开。即使我注释掉了睡眠和打开显示器的部分。都一样。
我认为当控制 returns 到主线程时监视器会打开。但事实并非如此。 运行 新话题中的这个片段导致相同的结果。
我也试过 powershell 脚本做同样的事情,它很有魅力。但不是 Java 片段。
我做错了什么?或者这到底有什么问题。
可能"doesn't work as expected"意味着你的期望是错误的。
值得注意的是,linked code, which I assume you used verbatim. The SendMessage 函数映射两次存在一些问题。第一个映射是正确的,但从未使用过:
LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
LPARAM paramLPARAM);
第二个映射将 int
替换为 WPARAM
,如果这里的某些结果异常,特别是在 64 位 system/JVM 上,我也不会感到惊讶。这是代码中调用的版本。
LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
LPARAM paramLPARAM);
除此之外,代码确实按预期工作,它 activates the power management features to turn the monitor off! The problem with your expectation is that you haven't considered what might turn the monitor back on. Moving or clicking a mouse (or perhaps releasing a pressed mouse button), pressing (or releasing) a key, network activity, or a variety of other things might generate system events which signal the power saving feature to turn the monitor back on。 (您可能会从 powershell 片段中看到相同的结果;在这种情况下,您的 mouse-click/keystroke 序列可能不同。)
使用this example of offing monitor with JNA 我遇到了以下问题: 显示器按预期关闭,但会在一秒钟内立即打开。即使我注释掉了睡眠和打开显示器的部分。都一样。
我认为当控制 returns 到主线程时监视器会打开。但事实并非如此。 运行 新话题中的这个片段导致相同的结果。
我也试过 powershell 脚本做同样的事情,它很有魅力。但不是 Java 片段。
我做错了什么?或者这到底有什么问题。
可能"doesn't work as expected"意味着你的期望是错误的。
值得注意的是,linked code, which I assume you used verbatim. The SendMessage 函数映射两次存在一些问题。第一个映射是正确的,但从未使用过:
LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
LPARAM paramLPARAM);
第二个映射将 int
替换为 WPARAM
,如果这里的某些结果异常,特别是在 64 位 system/JVM 上,我也不会感到惊讶。这是代码中调用的版本。
LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
LPARAM paramLPARAM);
除此之外,代码确实按预期工作,它 activates the power management features to turn the monitor off! The problem with your expectation is that you haven't considered what might turn the monitor back on. Moving or clicking a mouse (or perhaps releasing a pressed mouse button), pressing (or releasing) a key, network activity, or a variety of other things might generate system events which signal the power saving feature to turn the monitor back on。 (您可能会从 powershell 片段中看到相同的结果;在这种情况下,您的 mouse-click/keystroke 序列可能不同。)