2

.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環境でプログラミングしています。

助けてくれてありがとう。

4

1 に答える 1

2

私はこのソリューションをまったく同じ問題に使用しました。ネットで見つけたコードですが、自分のニーズに合わせて調整しました。

これをコード ウィンドウにドロップして、必要に応じて変更できるはずです。

Public Structure LASTINPUTINFO
    Public cbSize As Int32
    Public dwTime As Int32

End Structure

Declare Function GetLastInputInfo Lib "User32.dll" (ByRef plii As LASTINPUTINFO) As Boolean

Private Sub IdleTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdleTimer.Tick


    If ReportingEntireClass = False Then
        Dim LII As New LASTINPUTINFO, TicksSinceLastInput As Int32 = 0

        LII.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(LII)

        If GetLastInputInfo(LII) Then TicksSinceLastInput = (Environment.TickCount - LII.dwTime)

        If TicksSinceLastInput >= IdleSeconds Then
            If IdleClosing = False Then
                IdleClosing = True
                Idle.ShowDialog() 'this is a little 
                                  'form that warns about the app closing.
            End If
        End If
    End If


End Sub
于 2012-11-22T03:10:15.883 に答える