0

私はすでにいくつかの sendkeys アプリケーションを作成して生活を楽にしていますが、ウィンドウを切り替えて sendkeys をオフにするのを忘れたときにイライラします。たとえば、私のアプリケーションは「123123」をメモ帳に送信します (現在のアクティブなアプリケーション) を停止するまで、msword に切り替えると機能しません。その後、メモ帳に戻すと、キーが再び送信されます。それは可能ですか? のような議論があれば人生

If activeWindows = notepad.exe then
    do this
Else
    do nothing
End If

それは単なるアイデアですが、vb.netでそのようなものを作成する方法がわかりません。アプリケーションのタイトルの代わりに.exeを使用したいので、私がやっていることには非常に適していると思います.

thx事前に

4

2 に答える 2

2

GetForegroundWindowおよびGetWindowThreadProcessIdAPI を使用して、これを行うことができます。簡単な例を次に示します。

''' <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
''' <returns>The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation. </returns>
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
                      ByRef lpdwProcessId As Integer) As Integer
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Task.Run(AddressOf KeySender)

End Sub

Private Sub KeySender()
    While (True)

        Dim fgWin = GetForegroundWindow()
        Dim fgPid As New Integer()
        GetWindowThreadProcessId(fgWin, fgPid)

        Dim proc = System.Diagnostics.Process.GetProcessById(fgPid)
        Console.WriteLine(proc.ProcessName)

        If (proc.ProcessName = "notepad") Then
            SendKeys.SendWait("A")
        End If


        System.Threading.Thread.Sleep(1000)

    End While
End Sub
于 2013-04-09T02:15:53.633 に答える