How to detect Idling in different ways

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

How to detect Idling in different ways

Post 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
SPAMINATOR NR.1
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

Thanks, thats what I needed!
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
Post Reply