检查任何和所有键盘和鼠标的使用情况 [英] Check Any and All keyboard and mouse usage
问题描述
VB .NET 是否有任何检查方法来确保键盘和鼠标没有被使用?我知道有一种方法可以测试何时按下某个键或移动光标,但我想检查它们何时未被使用.
Does VB .NET have any way of checking to make sure the keyboard and mouse are not being used? I know there is a way to test when a certain key is pressed or the curser is moved but I want to check when they have not been used.
有什么想法吗?
推荐答案
不久前,我编写了一个自助服务终端应用程序,需要检测空闲时间.这是我正在使用的代码:
A while back I wrote a kiosk app where I needed to detect idle time. Here is the code that I am using:
Imports System.Runtime.InteropServices
Public Class Application
<StructLayout(LayoutKind.Sequential)> _
Private Structure LASTINPUTINFO
<MarshalAs(UnmanagedType.U4)> _
Public cbSize As Integer
<MarshalAs(UnmanagedType.U4)> _
Public dwTime As Integer
End Structure
<DllImport("user32.dll")> _
Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
Private idletime As Integer
Private lastInputInf As New LASTINPUTINFO()
Public Function GetIdleTime() As Integer
idletime = 0
lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
lastInputInf.dwTime = 0
If GetLastInputInfo(lastInputInf) Then
idletime = Environment.TickCount - lastInputInf.dwTime
End If
If idletime > 0 Then
Return CType(idletime / 1000, Integer)
Else
Return 0
End If
End Function
End Class
此代码应该允许您确保没有使用键盘和鼠标?"
This code should allow you "to make sure the keyboard and mouse are not being used?"
您将把这段代码放到一个类中.根据您的应用程序的需要,您将调用 GetIdleTime(),它返回应用程序空闲的秒数.此方法是 Windows API 调用的包装器.所以在你 5 分钟的例子中,你会写:
You will place this code into a class. As needed in your app, you will call GetIdleTime() which returns the number of seconds the app has been idle. This method is a wrapper around a Windows API call. So in your example of 5 minutes, you would write:
Dim a = New Application()
...
If a.GetIdleTime() > 300 Then
'do something useful
End If
这篇关于检查任何和所有键盘和鼠标的使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!