The function I want to achieve is to call the program when there is no keyboard input, mouse movement or click action. The first thing that comes to mind is to use hook HOOK to get keyboard or mouse actions, and call the program if there is no action. The result of my attempts is that there is always a problem when hooking and hooking.
Later, I thought that the screen saver of Windows enters the screen saver when the keyboard and mouse do not move, so I changed my mind and wanted to make the program in such a form. When the keyboard and mouse do not move, the system enters the screen saver, and then detects whether the system is running the screen saver. , the program is called if it is running. This method uses the screen saver as an intermediary and leaves the work of detecting keyboard and mouse movements to the screen saver. SystemParametersInfo can implement functions to obtain screen saver information. The reference code is as follows:
'API calls and common definitions: Private Declare Function SystemParametersInfo _ Lib "user32" _ Alias "SystemParametersInfoA" _ (ByVal uiAction As Long, _ ByVal uiParam As Long, _ pvParam As Any, _ ByVal fWInIni As Long) As Boolean Private Const SPI_GETSCREENSAVEACTIVE As Long = &H10 'Constant for whether the screensaver is enabledPrivate Const SPI_GETSCREENSAVERRUNNING As Long = &H72 'Constant for whether the screensaver is runningPrivate Sub Timer1_Timer() Dim bRunning As Boolean 'Variable for whether the screensaver is running, of course you can define the global variable SystemParametersInfo SPI_GETSCREENSAVERRUNNING, 0, bRunning, False 'Call API, bRunning returns the screen saver running status Debug.Print Time; "Screen saver running="; bRunning 'Demo: print information about whether the screen saver is running End Sub 'In addition, to check whether the screen saver is enabled, you can also use the following method: SystemParametersInfo SPI_GETSCREENSAVEACTIVE, 0 , bActive, False 'bActive is the return value (logical type)
But I don’t know why I still have problems debugging under WIN7, prompting SystemParametersInfo SPI_GETSCREENSAVERRUNNING, 0, bRunning, False
The bRunning type in is wrong and can only be given up.
Finally, the final solution is to use the GetLastInputInfo function to obtain the system’s idle time. The reference code is as follows:
Option ExplicitPrivate Declare Function GetLastInputInfo Lib "user32" (plii As LASTINPUTINFO) As BooleanPrivate Declare Function GetTickCount Lib "kernel32" () As LongPrivate Type LASTINPUTINFO cbSize As Long dwTime As LongEnd Type Private Sub Form_Load() Timer1.Interval = 1000 End Sub Private Sub Timer1_Timer() Dim lii As LASTINPUTINFO lii.cbSize = Len(lii) If GetLastInputInfo(lii) Then If (GetTickCount - lii.dwTime) / 60000 >= 15 Then Call MsgBox("Because the machine has not operated for 15 minutes, if 3 minutes If there is no response, the system will be forced to shut down", vbYesNo + vbExclamation + vbDefaultButton2, "prompt") End If End IfEnd SubThe above is the entire content of this article, I hope you all like it.