|
-
Nov 5th, 2007, 12:16 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Are any keys pressed?
I'm writing a program just now that will shutdown my computer if it hasn't been used in a while. Is there any code that will check to see if any keys are being pressed?
If a key is pressed I want to set a variable to 0. I have tried using GetAsyncKeyState but it didn't want to work in a loop. 
It doesn't matter what keys are pressed. Just if any are. Thanks.
-
Nov 5th, 2007, 12:26 PM
#2
Frenzied Member
Re: Are any keys pressed?
-
Nov 5th, 2007, 12:56 PM
#3
Thread Starter
Frenzied Member
Re: Are any keys pressed?
Thanks for the reply. I used this code but I was wondering if you can tell me what each line means? I'm not sure of the whole API thing. Lol.
Code:
Private Type LASTINPUTINFO
cbSize As Long
dwTime As Long
End Type
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function GetLastInputInfo Lib "user32" (plii As Any) As Long
Private Sub Timer1_Timer()
Dim lii As LASTINPUTINFO
lii.cbSize = Len(lii)
Call GetLastInputInfo(lii)
Label1.Caption = "system idle (secs): " & FormatNumber((GetTickCount() - lii.dwTime) / 1000, 2)
Label1.Refresh
End Sub
I understand most of it. Its really just the API's.
-
Nov 5th, 2007, 11:06 PM
#4
Frenzied Member
Re: [RESOLVED] Are any keys pressed?
Actually I dont undestand it either. lol
But the original Article gives the explanation.
GetLastInputInfo retrieves the time of the last system input event (mouse or keyboard action) into a LASTINPUTINFO structure. The key member of this type is the dwTime member, which holds the tick count of the last input. This API is typically called as needed to determine how long since the last keyboard or mouse input.
In this demo the code is in a Timer event in order to show how the time changes as interaction with Windows is made. The value returned by in dwTime (when the last event occurred) is subtracted from the the value returned by GetTickCount (the current tick count of the system). The difference is the number of milliseconds that the system has been idle. Note that the Timer code uses FormatNumber, so pre-VB6 devs need to use a different Format function.
After completing the form below, hit run and then don't move the mouse. The label will update as the idle time accumulated; clicking or moving the mouse, or pressing any key will cause the idle time to reset. The command button can be used to suspend/reactive the timer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|