I am using the following to send a string to the application on the foreground. But it's no good, as it for some reason only accepts numbers, and letters in uppercase. But I also need to send letters in lowercase, and slashes and backslashes etc. Can anyone help me achieve this? I also tried simply copying the string to the clipboard with the intention of finding out how to emulate Ctrl+V keypresses but it wouldn't copy the text to the clipboard in backgroundworker, which I use in my app. Said something about STAThread being required. So now it's back to trying to get this to work.

Private Sub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
        Dim input As New NativeMethods.INPUT
        Dim ki As New NativeMethods.KEYBDINPUT
        input.dwType = NativeMethods.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(NativeMethods.INPUT))
        Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
        If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
    End Sub

Private Sub but1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles but1.Click
        AppActivate("Notepad")
        Dim message As String = "HELLO WORLD"
        For Each c As Char In message
            Dim key As UInteger = Convert.ToInt16(c)
            DoKeyBoard(0, key)  ' key down is 0 - no flag...
            DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, key)
        Next
        DoKeyBoard(0, Keys.Decimal) ' example using keys enum which I think matches the VKEY values
        DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Decimal)
END SUB

Public Class NativeMethods

    <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

    <DllImport("user32.dll")> _
    Public Shared Function GetAsyncKeyState(ByVal vKey As Int32) As Short
    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
End Class

this is the right way to do it:

System.Windows.Forms.SendKeys.Send("Your Text Here!")

SendKeys seems to work indeed, for most characters anyway. For the others I use virtual keys. Not really a superb solution as it's keyboard-region specific but oh well it suits my current needs.

SendKeys seems to work indeed, for most characters anyway. For the others I use virtual keys. Not really a superb solution as it's keyboard-region specific but oh well it suits my current needs.

send keys works for all keyboard buttons but you can't send multiple at the same time.

the key for enter is {Enter} and the key for space is {Space} and so on... you can't put those ones in a context, each one needs it's oun sendkeys line.

You are right Andy, but if you want a key to be held down for a period of time rather than pressed once, or send a colon (which requires shift to be held down or you get ; instead of : ) you need to use virtualkeys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.