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.InteropServ ices.Marsh alAs(Syste m.Runtime. InteropSer vices.Unma nagedType. 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/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 050"'
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
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.InteropServ
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/co
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")
Thanks
MajinLoki
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you use System.Windows.Forms.SendK eys 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:\Hell oWorld.txt ")
' Send Alt S to save the file.
SendKeys.SendWait("%S")
// Send the enter key;
SendKeys.Send("{ENTER}");
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:\Hell
' Send Alt S to save the file.
SendKeys.SendWait("%S")
// Send the enter key;
SendKeys.Send("{ENTER}");
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