Question send keystroke with sendinput

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
is there something wrong with this? its not sending the key

VB.NET:
                Dim inputEvents As New USER32.INPUT

                inputEvents.ki.wVk = Convert.ToInt16(key)
                inputEvents.ki.wScan = 0
                inputEvents.ki.time = 0
                inputEvents.ki.dwExtraInfo = IntPtr.Zero
                Dim cbSize As Integer = Runtime.InteropServices.Marshal.SizeOf(GetType(USER32.INPUT))

                inputEvents.ki.dwFlags = USER32.KEYEVENTF.KEYDOWN '0
                USER32.SendInput(1, inputEvents, cbSize)
                inputEvents.ki.dwFlags = USER32.KEYEVENTF.KEYUP '2
                USER32.SendInput(1, inputEvents, cbSize)
 
Last edited:
Tried implementing your solution, after being disappointed with keybd_event, and cant make it work, probably because of the type (of some sort) not being set, what type were you referring to?

Structure for sendinput? something else?

Please elaborate, thanks.
 
Thanks, I have made it work, but I encountered a strange error along the way, truth be told the keybd_event produces the same error as sendinput:

even though it does work in notepad flawlessly, it does not work reliable unter IE8 (tested).
This is what I have
VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Posalji.Click
        AppActivate(TextBox1.Text)
        Dim message As String = "HH"
        Dim i
        For i = 0 To Len(message) - 1
            Dim key = char2code(message(i))
            BlockInput(True)
            DoKeyBoard(0, VK_SHIFT)
            DoKeyBoard(0, key)  ' key down is 0 - no flag...
            DoKeyBoard(KEYEVENTF.KEYUP, key)
            DoKeyBoard(KEYEVENTF.KEYUP, VK_SHIFT)
            BlockInput(False)
        Next
    End Sub

This sends the string containing two CAPITALIZED characters "H" to another window, and as I said earlier it works in notepad, but not in IE8.

VB.NET:
Private Sub DoKeyBoard(ByVal flags As KEYEVENTF, ByVal key As Int16)
        Dim input As New INPUT
        Dim ki As New KEYBDINPUT

        input.dwType = InputType.Keyboard
        input.ki = ki
        input.ki.wVk = Convert.ToInt16(key)
        input.ki.wScan = 0
        input.ki.time = 0
        input.ki.dwFlags = flags
        input.ki.dwExtraInfo = IntPtr.Zero
        Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT))
        Dim result As Integer = SendInput(1, input, cbSize)
        If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
    End Sub

And finally:

VB.NET:
Public Function char2code(ByVal slovo As Char) As Int16
        Dim x = slovo
        Select Case x
            Case "H"
                Return Keys.H
            Case "e"
                Return Keys.E
            Case "l"
                Return Keys.L
            Case "o"
                Return Keys.O

        End Select

    End Function


The pinvokes are as follows:

VB.NET:
<DllImport("user32.dll", SetLastError:=True)> _
     Friend Shared Function SendInput(ByVal cInputs As Int32, ByRef pInputs As INPUT, ByVal cbSize As Int32) As Int32
    End Function

    <StructLayout(LayoutKind.Explicit, pack:=1, Size:=28)> _
    Friend Structure INPUT
        <FieldOffset(0)> Public dwType As InputType
        <FieldOffset(4)> Public mi As MOUSEINPUT
        <FieldOffset(4)> Public ki As KEYBDINPUT
        <FieldOffset(4)> Public hi As HARDWAREINPUT
    End Structure

    <StructLayout(LayoutKind.Sequential, pack:=1)> _
    Friend Structure MOUSEINPUT
        Public dx As Int32
        Public dy As Int32
        Public mouseData As Int32
        Public dwFlags As MOUSEEVENTF
        Public time As Int32
        Public dwExtraInfo As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential, pack:=1)> _
    Friend Structure KEYBDINPUT
        Public wVk As Int16
        Public wScan As Int16
        Public dwFlags As KEYEVENTF
        Public time As Int32
        Public dwExtraInfo As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential, pack:=1)> _
    Friend Structure HARDWAREINPUT
        Public uMsg As Int32
        Public wParamL As Int16
        Public wParamH As Int16
    End Structure

    Friend Enum InputType As Integer
        Mouse = 0
        Keyboard = 1
        Hardware = 2
    End Enum

    <Flags()> _
    Friend Enum MOUSEEVENTF As Integer
        MOVE = &H1
        LEFTDOWN = &H2
        LEFTUP = &H4
        RIGHTDOWN = &H8
        RIGHTUP = &H10
        MIDDLEDOWN = &H20
        MIDDLEUP = &H40
        XDOWN = &H80
        XUP = &H100
        VIRTUALDESK = &H400
        WHEEL = &H800
        ABSOLUTE = &H8000
    End Enum

    <Flags()> _
    Friend Enum KEYEVENTF As Integer
        EXTENDEDKEY = 1
        KEYUP = 2
        [UNICODE] = 4
        SCANCODE = 8
    End Enum



    Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As Long


Any idea why both send input and keybd_event are so unreliable under IE8 and why in fact this happens at all?
In IE8, if I spam the "HH" string I get hhhhhhhhhhHHhhhhhhhhHHhhhhhhhhHHHHhh

?

Thanks in advance!
 
Back
Top