Results 1 to 5 of 5

Thread: Writing a program that uses idle time

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    13

    Writing a program that uses idle time

    Hi! I'm the new kidd in the block.
    I want to write a program that could use idle time of my computer.
    I got the general idea about it but no clue about how to start.

    The functionality is similar to screen savers applications. I need a program that continously monitors keyboard activity and if there is no activity in an specified time period the program starts a routine of backing up files.

    My first obstacle is: How to monitor keyboard or mouse activity while running another application.

    My second obstacle is: Once my program has detected n-minutes without keyboard or mouse activity I would like to confirm that no body is at the keyboard issuing a confirmation message prior to start backing up the files. If there is no answer in x-seconds the program should start the backing up routine.

    I think I'll find more obstacles, but one step a time (well two).

    Thanks for your hints ..

    QuijoteMX

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Writing a program that uses idle time

    Welcome!

    To get started, you can use the GetAsyncKeyState API to check for keys pressed. Also you can use the GetCursorPos API to check for mouse moving.

    Other ways (harder ways) are installing keyboard/mouse hooks.


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    13

    Re: Writing a program that uses idle time

    Thanks for your answer. I did a search on getasynckeystatus and found the following snippet from Jacob Roman

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    3. Public Key As Integer
    4. Private Sub Form_Unload(Cancel As Integer)
    5.     Unload Me
    6. End Sub
    7.  
    8. Private Sub Timer1_Timer()
    9.     Key = GetAsyncKeyState(46)
    10.     If Key = -32767 Then
    11.         MsgBox "You Pressed Delete", vbExclamation        
    12.     End If
    13. End Sub

    I tried it and works fine but only for delete key pressing. I need to detect any key pressing. I want to trigger a routine once x-seconds have elapsed since last key input ..

    Is that posible using getasynckeystatus ?

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Writing a program that uses idle time

    Have a look at this. It's far from perfect, but it's a start :

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    3. Public Key As Integer
    4. Dim iTime As Integer
    5.  
    6. Private Sub Form_Load()
    7.     tmrKeys.Interval = 10
    8.     tmrKeys.Enabled = True
    9.    
    10.     tmrCount.Interval = 1000
    11.     tmrCount.Enabled = True
    12.    
    13.     iTime = 5 'seconds
    14. End Sub
    15.  
    16. Private Sub Form_Unload(Cancel As Integer)
    17.     Unload Me
    18. End Sub
    19.  
    20. Private Sub tmrCount_Timer()
    21.     Static iCount As Integer
    22.    
    23.     iCount = iCount + 1
    24.    
    25.     If iCount = iTime Then
    26.         MsgBox "5 seconds idle"
    27.         iCount = 0
    28.     End If
    29. End Sub
    30.  
    31. Private Sub tmrKeys_Timer()
    32.     Dim i As Integer
    33.    
    34.     For i = 0 To 255
    35.         Key = GetAsyncKeyState(i)
    36.         If Key = -32767 Then
    37.             tmrCount.Enabled = False
    38.             tmrCount.Enabled = True
    39.             MsgBox "You Pressed Delete", vbExclamation
    40.         End If
    41.     Next
    42. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Writing a program that uses idle time

    I would use a global keyboard hook (in VB you need a journal hook for that) and poll the time between keyboard activity, resetting the timer whenever you receive a hook event.

    Demo attached:

    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width