Java 检测 Windows 上的不活动

标签 java winapi jna

我找到了以下代码来检测不活动;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.sun.jna.*;
import com.sun.jna.win32.*;
import java.util.List;

/**
 * Utility method to retrieve the idle time on Windows and sample code to test it.
 * JNA shall be present in your classpath for this to work (and compile).
 * @author ochafik
 */
public class Win32IdleTime {

    public interface Kernel32 extends StdCallLibrary {
        Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);

        /**
         * Retrieves the number of milliseconds that have elapsed since the system was started.
         * @see http://msdn2.microsoft.com/en-us/library/ms724408.aspx
         * @return number of milliseconds that have elapsed since the system was started.
         */
        public int GetTickCount();
    };

    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

        /**
         * Contains the time of the last input.
         * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputstructures/lastinputinfo.asp
         */
        public static class LASTINPUTINFO extends Structure {
            public int cbSize = 8;

            /// Tick count of when the last input event was received.
            public int dwTime;

            @
            Override
            protected List getFieldOrder() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        }

        /**
         * Retrieves the time of the last input event.
         * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getlastinputinfo.asp
         * @return time of the last input event, in milliseconds
         */
        public boolean GetLastInputInfo(LASTINPUTINFO result);
    };

    /**
     * Get the amount of milliseconds that have elapsed since the last input event
     * (mouse or keyboard)
     * @return idle time in milliseconds
     */
    public static int getIdleTimeMillisWin32() {
        User32.LASTINPUTINFO lastInputInfo = new User32.LASTINPUTINFO();
        User32.INSTANCE.GetLastInputInfo(lastInputInfo);
        return Kernel32.INSTANCE.GetTickCount() - lastInputInfo.dwTime;
    }

    enum State {
        UNKNOWN, ONLINE, IDLE, AWAY
    };

    public static void main(String[] args) {
        if (!System.getProperty("os.name").contains("Windows")) {
            System.err.println("ERROR: Only implemented on Windows");
            System.exit(1);
        }
        State state = State.UNKNOWN;
        DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");

        for (;;) {
            int idleSec = getIdleTimeMillisWin32() / 1000;

            State newState =
                idleSec < 30 ? State.ONLINE :
                idleSec > 5 * 60 ? State.AWAY : State.IDLE;

            if (newState != state) {
                state = newState;
                System.out.println(dateFormat.format(new Date()) + " # " + state);
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ex) {}
        }
    }
}

但由于某种原因,我收到以下错误:

java.lang.UnsupportedOperationException: Not supported yet.

有什么想法可以解决这个问题吗?

最佳答案

您有一个名为 LASTINPUTINFO 的类,它扩展了抽象类 com.sun.jna.Structure 。正如 Stewart 和 Jim Garrison 所评论的,您对 getFieldOrder 的实现方法抛出 UnsupportedOperationException 异常。此方法实现可能是由您的 IDE 自动生成的。
当调用此方法以按正确顺序获取结构的字段名称时,会引发异常。

/**
* Contains the time of the last input.
* @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputstructures/lastinputinfo.asp
*/
public static class LASTINPUTINFO extends Structure {
    public int cbSize = 8;

    /// Tick count of when the last input event was received.
    public int dwTime;

    @
    Override
    protected List getFieldOrder() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

您可以通过正确实现 getFieldOrder 方法来解决此问题。

关于Java 检测 Windows 上的不活动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18859887/

相关文章:

java - 如何关闭特定玩家的聊天?

java - Glassfish Web 应用程序没有响应

java - 打印字符串,接受用户输入并使用私有(private)字符串方法验证它?

C win32 应用程序可以编译和链接,但除了进程之外什么也不显示给我。为什么?

windows - 识别重启

java - 如何在java中使用指纹sdk?是否有适用于所有指纹设备的通用 SDK?

JavaFX TabPane - 多个 Controller 和多个 FXML 文件

windows - SetWindowPos 如何影响所有者窗口的 Z 顺序?

java - java 中的 MAGIMAGEHEADER

java - 无法从加载器加载库