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
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.
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:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Key As Integer
Private Sub Form_Unload(Cancel As Integer)
Unload Me
End Sub
Private Sub Timer1_Timer()
Key = GetAsyncKeyState(46)
If Key = -32767 Then
MsgBox "You Pressed Delete", vbExclamation
End If
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 ?
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:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Key As Integer
Dim iTime As Integer
Private Sub Form_Load()
tmrKeys.Interval = 10
tmrKeys.Enabled = True
tmrCount.Interval = 1000
tmrCount.Enabled = True
iTime = 5 'seconds
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Me
End Sub
Private Sub tmrCount_Timer()
Static iCount As Integer
iCount = iCount + 1
If iCount = iTime Then
MsgBox "5 seconds idle"
iCount = 0
End If
End Sub
Private Sub tmrKeys_Timer()
Dim i As Integer
For i = 0 To 255
Key = GetAsyncKeyState(i)
If Key = -32767 Then
tmrCount.Enabled = False
tmrCount.Enabled = True
MsgBox "You Pressed Delete", vbExclamation
End If
Next
End Sub
1 Attachment(s)
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: