B4J Question Windows user idle time

MarrowZero

Member
Licensed User
Hi peoples, is there a modern library or method for detecting when windows user is idle?

There is this old library, but I didn't find anything 'modern'


Thanks
 

MarrowZero

Member
Licensed User
I've trawled my way through the forum, but so far all examples are old/non-working.

I'd be grateful for a point in the right direction or maybe there a general example of how to call the windows api that can be modified

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
#AdditionalJar: jna-5.8.0 
#AdditionalJar: jna-platform-5.8.0 
Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Test
    StartMessageLoop
End Sub

Sub Test
    For i = 1 To 10
        Log(GetIdleTime)
        Sleep(1000)
    Next
End Sub

'Returns the idle time in milliseconds.
Sub GetIdleTime As Int
    Dim LASTINPUTINFO  As JavaObject
    LASTINPUTINFO .InitializeNewInstance("com.sun.jna.platform.win32.WinUser.LASTINPUTINFO", Null)
    Dim User32 As JavaObject
    User32 = User32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
    User32.RunMethod("GetLastInputInfo", Array(LASTINPUTINFO ))
    Dim Kernel32 As JavaObject
    Kernel32 = Kernel32.InitializeStatic("com.sun.jna.platform.win32.Kernel32").GetField("INSTANCE")
    Dim CurrentTicks As Int = Kernel32.RunMethod("GetTickCount", Null)
    Dim LastInput As Int = LASTINPUTINFO.GetField("dwTime")
    Dim delta As Int = CurrentTicks - LastInput
    Return delta
End Sub

You need to download the two jars and put them in the additional libraries folder:
 
Upvote 0
Top