Is there a cross-platform way of obtaining the title of the current window on display using Kotlin multiplatform?

131 Views Asked by At

For example the following code snippet does just that but only for Windows:

import com.sun.jna.platform.win32.User32
import com.sun.jna.platform.win32.WinDef.HWND

object ActiveWindowFinder {
    @JvmStatic
    fun main(args: Array<String>) {

        var count = 0
        while (true){
            val hwnd: HWND = User32.INSTANCE.GetForegroundWindow()
            val title = CharArray(150)
            User32.INSTANCE.GetWindowText(hwnd, title, 150)
            println("Active window title: ${String(title).trim()}")

            count ++
            Thread.sleep(200)
            if (count == 100){
                break
            }
        }

    }
}

I need a way of getting the same behaviour on Mac and on Linux.

1

There are 1 best solutions below

0
gavr On

No, there is no cross-platform solution for that. Each platform has it's own window system, and linux has 2: Wayland and XOrg. This question is not related to kotlin or kotlin multiplatform. Search it for java, for example Get current active window's title in Java