Simulate keyboard input

Posted on 2005-04-13
Last Modified: 2008-01-09
I am trying to write a module where I can send a string or and ascii character code and then that string/code is then simulated as keyboard input.  Here is some of my sample code:

Module Keyboard
    ' Declare Type for API call:

    Private Structure OSVERSIONINFO
        Dim dwOSVersionInfoSize As Integer
        Dim dwMajorVersion As Integer
        Dim dwMinorVersion As Integer
        Dim dwBuildNumber As Integer
        Dim dwPlatformId As Integer
        <VBFixedString(128), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=128)> Public szCSDVersion As String '  Maintenance string for PSS usage
    End Structure

    ' API declarations:
    'UPGRADE_WARNING: Structure OSVERSIONINFO may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Integer

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    Private Declare Function GetKeyboardState Lib "user32" (ByRef pbKeyState As Byte) As Integer

    Private Declare Function SetKeyboardState Lib "user32" (ByRef lppbKeyState As Byte) As Integer

    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer

    Const KEYEVENTF_KEYUP As Short = &H2S

    Public Sub keyPress(ByVal ascii As Integer)
    End Sub

    Public Sub printText(ByVal text As String)
        Dim bits() As Char
        Dim c As Char
        bits = text.ToCharArray()

        For Each c In bits
    End Sub

    Private Sub pressKey(ByVal key As Integer)
        'Simulate Key Press
        keybd_event(key, MapVirtualKey(key, 0), 0, 0)
        'Simulate Key Release
        keybd_event(key, MapVirtualKey(key, 0), KEYEVENTF_KEYUP, 0)
    End Sub
End Module

Now, when I call Keyboard.keyPress(13), it correctly presses the enter key.  Keyboard.keyPress(Asc("a")) however, does not work correctly.  What do I need to change to get this to simulate keboard output of strings sent to PrintText?

Question by:MajinLoki
    LVL 12

    Accepted Solution

    I tried your code using

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        End Sub

    and it worked fine   (lil difference is I use AscW  )
    LVL 12

    Expert Comment

    so maybe your sub should be:

        Public Sub printText(ByVal text As String)
            Dim bits() As Char
            Dim c As Char
            bits = text.ToCharArray()

            For Each c In bits
        End Sub
    LVL 11

    Expert Comment

    Why don't you use System.Windows.Forms.SendKeys class for your purpose.

    here is the examples

    AppActivate("Untitled - Notepad")
    SendKeys.SendWait("Hello world!")
    ' Send Ctrl S to open the Save As dialog box.
    ' Send the name of the new file.
    ' Send Alt S to save the file.
    // Send the enter key;


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now