• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

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
0
MajinLoki
Asked:
MajinLoki
  • 2
1 Solution
 
S-TwilleyCommented:
I tried your code using

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Focus()
        Application.DoEvents()
        Keyboard.keyPress(AscW("A"))
    End Sub

and it worked fine   (lil difference is I use AscW  )
0
 
S-TwilleyCommented:
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
0
 
jatinderalaghCommented:
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}");

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now