User Detection - The Next Generation

Windows specific forum
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6121
Joined: Sat May 17, 2003 11:31 am
Contact:

User Detection - The Next Generation

Post by blueznl »

Some older USB devices and device drivers worked without problems on anything before W10, and actually still work on Win10.

However, for some reason, when connecting such a device to a Win10 PC it appears to confuse the 'no user / user away' detection a bit.

If I connect my Jog Shuttle Pro, the program below keeps updating the 'last user activity' field. Is there an alternative way to detect if the user is gone?

(Why I need this: I change wallpapers preferably with the user NOT around, so any processing won't cause a 'hickup' during work.)

My old code is based on this:

Code: Select all

x.LASTINPUTINFO
x\cbSize = SizeOf(x)
;
OpenWindow(1,10,10,150,50,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
StringGadget(1,10,20,130,20,"")
;
Repeat
  WaitWindowEvent(1024)
  GetLastInputInfo_(@x)
  SetGadgetText(1,Str(x\dwTime))
Until Event() = #PB_Event_CloseWindow
Maybe I should add a CPU load detection, or something alse. Any suggestions for newer, smarter code welcome! :-)
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
dige
Addict
Addict
Posts: 1123
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: User Detection - The Next Generation

Post by dige »

Maybe by manualy tracking of keyboard events or mouse movements?
Or by checking window messages with WinCallBack() like #WM_QUERYENDSESSION, #WM_WTSSESSION_CHANGE
"Daddy, I'll run faster, then it is not so far..."
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6121
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: User Detection - The Next Generation

Post by blueznl »

Is there a function that would return average processor load or something similar?
( PB5.73LTS Win10 x64 Asrock AB350 Pro4 Ryzen 1600X GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Bitblazer
Enthusiast
Enthusiast
Posts: 497
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: User Detection - The Next Generation

Post by Bitblazer »

Maybe something like Event tracing for windows on any HID device (mouse, keyboard, others)?
dige
Addict
Addict
Posts: 1123
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: User Detection - The Next Generation

Post by dige »

blueznl wrote: Thu Nov 25, 2021 12:21 am Is there a function that would return average processor load or something similar?
Please remind, the processor load can also increase due to virus scans or windows cleanup tasks..

Code: Select all

Import "kernel32.lib"
  GetSystemTimes(*lpIdleTime, *lpKernelTime, *lpUserTime)
  GlobalMemoryStatus(*GMSEX)
EndImport

Procedure.s RAMLoad ()
  Protected Memory.MEMORYSTATUSEX
  
  GlobalMemoryStatus(@Memory)
  
  ProcedureReturn Str(Memory\dwMemoryLoad) + " %"
  
EndProcedure

Procedure.s CPULoad(accuracy = 0)
  Static FirstRun = #True
  Static Dim StartTimes(1), Dim EndTimes(1)
  Static CPU_Kernel.FILETIME, CPU_User.FILETIME, CPU_Idle.FILETIME
  Protected Idle_Time.i
  Protected Total_Time.i
  Protected Text.s
  
  GetSystemTimes(@CPU_Idle, @CPU_Kernel, @CPU_User)
  EndTimes(0) = (CPU_Kernel\dwLowDateTime + CPU_User\dwLowDateTime)
  EndTimes(1) = CPU_Idle\dwLowDateTime
  
  If FirstRun
    StartTimes(1) = EndTimes(1)
    StartTimes(0) = EndTimes(0)
    FirstRun = #False
    ProcedureReturn ""
  EndIf
  
  Idle_Time = EndTimes(1) - StartTimes(1)
  
  If Idle_Time > 0
    Total_Time = EndTimes(0) - StartTimes(0)
    Text = StrF( ((Total_Time - Idle_Time) / Total_Time) * 100 , accuracy ) + " %"
  EndIf
  
  StartTimes(1) = EndTimes(1) 
  StartTimes(0) = EndTimes(0) 
  
  ProcedureReturn Text 
EndProcedure 

CompilerIf #PB_Compiler_IsMainFile
  
  Repeat
    Debug "CPU: " + CPULoad(0)
    Debug "RAM: " + RAMLoad()
    Delay(400)
  ForEver
CompilerEndIf
"Daddy, I'll run faster, then it is not so far..."
Post Reply