B4J Question how to get getforegroundwindow / getactivewindow


Active Member
Licensed User
Longtime User
I am trying to build a tool that would monitor user activity. I would like to know if there is any way to get the active window's process, title and time spent on that application
I came across articles that suggests to use JNA

I have achieved the following if anyone is interested
Browser History
Screenshots ( dual monitor included )
Hostname . current loggedin user
Open Processes
Idle time

If there is anything that you might think i will need to add, please suggest.

any help would be appreciated. thank you


Active Member
Licensed User
Longtime User
Thank you. Here is the code if someone else needs it

    Dim user32 As JavaObject
    user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
    Dim hwnd1 As JavaObject = user32.RunMethod("GetForegroundWindow",Null)
    Dim title(512) As Char
    user32.RunMethod("GetWindowText", Array(hwnd1,title,512))
    Dim native As JavaObject
    Dim activewindowtitle As String = native.InitializeStatic("com.sun.jna.Native").RunMethod("toString", Array(title))
    activewindowtitle = activewindowtitle.Trim
Upvote 0