Search code examples
vb.netmdimdichildsendinput

how do I use sendinput on an mdi child window in external application?


I am trying to use the sendinput API on an mdi child window in an external application to send keys/mouse input to it for mathematical operations. I have the window handle id and use setfocus windows API to setfocus on it but sendinput will not send the keys. I tab to the receiving controls which allow numeric values. In addition, I need some numeric values to be sent as decimal input such as 3.3. If you need an image of the external application, please comment below and I will post an image on my google drive account.

......
Dim hNoteThread As IntPtr = GetWindowThreadProcessId(windowids(foundid), IntPtr.Zero)
    If hNoteThread <> 0 Then
        MsgBox("Panel 0 found for input")
    End If
    ' SetForegroundWindow(windowids(foundid))
    If AttachThreadInput(GetCurrentThreadId(), hNoteThread, True) = True Then
        SetFocus(windowids(foundid))
        System.Threading.Thread.Sleep(1000)
        SendKey(VK_TAB)
        System.Threading.Thread.Sleep(2000)
        SendKey(VK_NUMPAD2)
        System.Threading.Thread.Sleep(2000)
        SendKey(VK_TAB)
        SendKey(VK_NUMPAD0 + VK_DECIMAL + VK_NUMPAD5)
        ' System.Threading.Thread.Sleep(1000)
        SendKey(VK_TAB)
        SendKey(VK_NUMPAD1)
        ' System.Threading.Thread.Sleep(1000)
        SendKey(VK_TAB)
        SendKey(VK_NUMPAD2)
        System.Threading.Thread.Sleep(2000)
        MsgBox("keys sent")
    Else
        MsgBox("keys not sent")
    End If

    Private Sub SendKey(ByVal bKey As Short)
    Dim GInput(1) As INPUT

    ' press the key
    GInput(0).type = INPUT_KEYBOARD
    GInput(0).ki.wVk = bKey
    GInput(0).ki.dwFlags = 0

    ' release the key
    GInput(1).type = INPUT_KEYBOARD
    GInput(1).ki.wVk = bKey
    GInput(1).ki.dwFlags = KEYEVENTF_KEYUP


    SendInput(2, GInput, Marshal.SizeOf(GetType(INPUT)))

End Sub

Edit: here's a link to an image so someone can figure out how the input needs to be sent: https://drive.google.com/file/d/0BwIppJC0dxJBalp3Ykd2VzZkQ0E/edit?usp=sharing

The target controls for sending input are the Psi, D, and the other values that can be changed in Panel0 and I need to click the button too.

The problem is that send input fails to work at all and my message box displays saying that the input has been sent.


Solution

  • I figured out that the button was a application drawn button so it had no handle! Also, the other controls may have been drawn too. I feel so dumb. I ended up getting the handle of the mainform and subform then tabbing through the different controls except for the button. For the button: I used the RECT class to get the bottom leftcorner of the window and kept moving x until I reached the farest left corner of the button while using sendinput to click. Please comment below if you want source. Its too big to post here.