Find out what application (window) is in focus in Java

前端 未结 3 1643
温柔的废话
温柔的废话 2020-11-28 11:00

I\'d like to know how I can code a Java program that knows which Windows application is in focus. I can have many windows open but I want to know the one that\'s being used

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 11:45

    I'm afraid there's no java api for that. JVM does not know anything about the windows it does not manage. You'll probably have to use JNI and call this function

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
    

    MSDN link

    PS. THere is a GetWindowText function that you might want to use if you need to grab the title of the window.

    This post has JNI examples that might be helpful for you.

提交回复
热议问题