Link to home
Start Free TrialLog in
Avatar of MajinLoki
MajinLoki

asked on

Simulate keyboard input

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_EXTENDEDKEY As Short = &H1S
    Const KEYEVENTF_KEYUP As Short = &H2S

    Public Sub keyPress(ByVal ascii As Integer)
        pressKey(ascii)
    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
            pressKey(Asc(c))
        Next
    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?

Thanks
MajinLoki
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of S-Twilley
S-Twilley

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
            pressKey(AscW(c))
        Next
    End Sub
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.
SendKeys.SendWait("^S")
' Send the name of the new file.
SendKeys.SendWait("c:\HelloWorld.txt")
' Send Alt S to save the file.
SendKeys.SendWait("%S")
// Send the enter key;
SendKeys.Send("{ENTER}");