2016-05-25 5 views
0

Ich mache Anwendung in vb.net, die, wenn ran Tastatureingaben von Tastatur in allen Editoren erkennt. Jetzt kann ich Tastenanschläge bekommen, das ist ein einfacher Teil, aber ich möchte den Namen der Anwendung kennen, wenn der Benutzer im Editor schreibt, dann möchte ich diesen Namen. Bitte kann jemand eine Idee vorschlagen. DankGet Application Name Von Tastatureingabe in VB.net

Antwort

0

Sie benötigen Windows-API verwenden:

Private Sub Main() 
    Dim shortName as String = GetShortEXEName(GetForegroundWindow()) 
End Sub 

' Define other methods and classes here 
<DllImport("user32.dll", SetLastError := True)> _ 
Public Shared Function GetForegroundWindow() As IntPtr 
End Function 

Public Shared Function GetProcess(hwnd As IntPtr) As Process 
    Dim intID As Integer = 0 
    GetWindowThreadProcessId(hwnd, intID) 
    Return Process.GetProcessById(intID) 
End Function 

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

Public Shared Function GetShortEXEName(hwnd As IntPtr) As String 
    ' this seems to be better to use than calling GetProcessEXEName and then using substring functions to get the short executable name 
    ' because that function seems to crash when dealing with Windows Explorer windows 
    Try 
     Return GetProcess(hwnd).ProcessName 
    Catch 
     Return "" 
    End Try 
End Function