.NETを使用してWindowsで実行されているすべてのアプリケーションからすべてのキーボードとマウスのイベントをキャッチする方法はありますか?
私はいくつかの同様の投稿を見つけました。最初はあなたが開発しているアプリケーションのためだけにこれを行う方法です:VB Detect Idle time
デスクトップがアイドル状態になっている時間を確認する方法を示す投稿と同様に:ユーザーが非アクティブであるかどうかを確認します
私が試したのは、基本的に10秒ごとにGetInactiveTimeを呼び出すメインフォームのタイマーを使用してその時間を記録し、CurrentInactiveTime<LastInactiveTimeのときにイベントを発生させることです。私が探しているのは、もう少しリアルタイムでもう少し正確なものです。
<StructLayout(LayoutKind.Sequential)> _
Public Structure LASTINPUTINFO
Public cbSize As UInteger
Public dwTime As UInteger
End Structure
<DllImport("user32.dll")> _
Friend Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
Public Shared Function GetInactiveTime() As TimeSpan
Dim info As LASTINPUTINFO = New LASTINPUTINFO()
info.cbSize = CUInt(Marshal.SizeOf(info))
If GetLastInputInfo(info) Then
Return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime)
Else
Return Nothing
End If
End Function
Sub Main()
inactiveTimer = New Timer()
inactiveTimer.Interval = 10000
inactiveTimer.Enabled = True
inactiveTimer.Start()
Dim tempTime As DateTime = Now
lastInactiveTime = tempTime - tempTime
End Sub
Private Sub inactiveTimer_Tick(sender As Object, e As EventArgs) Handles inactiveTimer.Tick
Dim currentInactiveTime As TimeSpan = GetInActiveTime()
Dim tempLastInactiveTime As TimeSpan = lastInactiveTime
lastInactiveTime = currentInactiveTime
If currentInactiveTime < tempLastInactiveTime Then
RaiseEvent SomeEvent
End IF
End Sub
また、私はWindows/VB.NET環境でプログラミングしています。
助けてくれてありがとう。