Page 1 of 1

How to detect Idling in different ways

Posted: Tue Feb 21, 2006 2:31 pm
by Rings
Code updated For 5.20+

After long time idling myself, nothing done in PB,
i tried to write a routine for that
Detect Idling - for V4

Code: Select all

; Detect Idling , Win32 w2k and above
; (c) 2006 By Siegfried Rings Purebasic 4
;
; ->i love undocumented Code :)
; 
Enumeration
  #Window_0
  #Output
  #StartDetectButton
  #EndDetectButton
  #TextGadget
EndEnumeration

; Structure LASTINPUTINFO 
;  cbSize.l
;  dwTime.l
; EndStructure

Procedure.s MillisSeconds2Time(Milliseconds)
 ProcedureReturn FormatDate("%hh:%ii:%ss",Milliseconds/1000)
 EndProcedure

#USER_IDLE_BEGIN = 1
#USER_IDLE_END = 2
Global Lib.l

Procedure IdleDetectionCallBack(dwState.l)
    Timestamp.s=MillisSeconds2Time(ElapsedMilliseconds())
    Select dwState
     Case #USER_IDLE_BEGIN
       AddGadgetItem(#Output,0,"Idle started "+ Timestamp)
       AddGadgetItem(#Output,0,"fire your screensaver here :)" )
     Case #USER_IDLE_END
       AddGadgetItem(#Output,0,"Idle ended "+ Timestamp)
     EndSelect
EndProcedure

Procedure BeginIdleDetection(pfnCallback, dwIdleMin.l, dwReserved.l )
If Lib=0
 Lib=LoadLibrary_("Msidle.dll")
 If Lib 
  adr=GetProcAddress_(Lib,3) 
  If adr
   If CallFunctionFast(adr,pfnCallback, dwIdleMin , dwReserved )=0
    AddGadgetItem(#Output,0,"Begin Idle Detection after "+Str(dwIdleMin )+" min.")
   EndIf
  Else
   Debug "Proc not found !" 
  EndIf
 EndIf
EndIf
EndProcedure
  
Procedure EndIdleDetection( dwReserved.l )
If Lib 
 adr=GetProcAddress_(Lib,4) 
 If adr
  If CallFunctionFast(adr,dwReserved)
     AddGadgetItem(#Output,0,"End Idle Detection")
  EndIf
 EndIf
 FreeLibrary_(Lib)
EndIf

EndProcedure

Procedure MyTimer( HWND ,uMsg,idEvent,   dwTime    )
 ;follow functions retreives the last input !
 glbLASTINPUTINFO.LASTINPUTINFO 
 glbLASTINPUTINFO\cbsize=8
 Result=GetLastInputInfo_(@glbLASTINPUTINFO)
 sdummy.s="last Input at " + MillisSeconds2Time(glbLASTINPUTINFO\dwTime)
 If GetGadgetText(#TextGadget)<>sdummy
  SetGadgetText(#TextGadget,sdummy)
 EndIf 
EndProcedure

If OpenWindow(#Window_0, 216, 100, 243, 300, "Detect IDLE'ing",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
;  If CreateGadgetList(WindowID(#Window_0))
  ListViewGadget(#Output, 10, 50, 220, 200)
  ButtonGadget(#StartDetectButton, 10, 10, 100, 30, "Start Detection")
  ButtonGadget(#EndDetectButton, 120, 10, 110, 30, "End Detection")
  TextGadget(#TextGadget, 10, 260, 220, 30, "Last Input")   
;  EndIf
 tproc=SetTimer_(0,0,1000,@MyTimer())
 MessageRequester("Info how to use","Click the Start-Detection Button , do nothing and wait until the callback fires",0)
 Repeat ; Start of the event loop
  Event = WaitWindowEvent() 
  WindowID = EventWindow() 
  GadgetID = EventGadget() 
  EventType = EventType() 
  Select GadgetID 
     Case #StartDetectButton
      BeginIdleDetection(@IdleDetectionCallBack(),1,0)
     Case #EndDetectButton
      EndIdleDetection(0)
   EndSelect
  
 Until Event = #PB_Event_CloseWindow ; End of the event loop
 If tproc
  KillTimer_( 0,tproc )
 EndIf 
EndIf

Posted: Fri Feb 24, 2006 11:45 pm
by FloHimself
Thanks, thats what I needed!