How can I make a key spammer for a specific software on vb.net

Frequency 1 Reputation point
2021-06-14T18:45:55.693+00:00

Hi,

I have managed to make my software to spam a certain letter or number, but i want it to target a specific window that is open which then it will allow me to utilise both of my monitors.

i am using vb.net

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,727 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 85,136 Reputation points
    2021-06-15T21:22:08+00:00

    You must activate and set focus to the destination window.
    For example, with Notepad =>

            Dim hWnd As IntPtr = FindWindow("Notepad", Nothing)
            If (hWnd <> IntPtr.Zero) Then
                SwitchToThisWindow(hWnd, 1)
                SendKeys.Send("x")
            End If
    

    Declarations :

    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function SwitchToThisWindow(hWnd As IntPtr, fAltTab As Boolean) As Boolean
    End Function
    
    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    
    1 person found this answer helpful.

  2. Frequency 1 Reputation point
    2021-06-22T17:26:27.977+00:00

    can anyone help me with this please

    0 comments No comments

  3. Castorix31 85,136 Reputation points
    2021-06-23T06:27:35.56+00:00

    im trying to spam the notepad while being able to browse my PC do you get me?

    You can use PostMessage, but it is not recommended...

    Same test with Notepad =>

    Dim hWndTarget As IntPtr = FindWindow("Notepad", Nothing)
    If (hWndTarget <> IntPtr.Zero) Then
        ' Get Edit control handle
        hWndTarget = GetWindow(hWndTarget, GW_CHILD)
        PostMessage(hWndTarget, WM_KEYDOWN, Keys.X, IntPtr.Zero)
    End If
    

    Declarations :

        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
        End Function
    
        Public Const WM_KEYDOWN = &H100
        Public Const WM_KEYUP = &H101
    
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function GetWindow(hWnd As IntPtr, uCmd As UInteger) As IntPtr
        End Function
    
        Public Const GW_HWNDFIRST = 0
        Public Const GW_HWNDLAST = 1
        Public Const GW_HWNDNEXT = 2
        Public Const GW_HWNDPREV = 3
        Public Const GW_OWNER = 4
        Public Const GW_CHILD = 5
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.