VB6.0 SendKeys Function Tutorial
VB6.0 SendKeys Function Tutorial
You can easily send keys to the active window, as if the user was pressing keys on the keyboard
by using the SendKeys statement.
The code above runs notepad, and sends the keys 'This is a test string' to it. If you want to use special
keys, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes
shown below:
Key Code
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code
with one or more of the following codes:
Key Code
SHIFT +
CTRL ^
ALT %
To specify that any combination of SHIFT, CTRL, and ALT should be held down while several
other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to
hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT
while E is pressed, followed by C without SHIFT, use "+EC".
To specify repeating keys, use the form {key number}. You must put a space between key and
number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means
press H 10 times.
VBSendKeys - Send keys to any Windows and MS-DOS application
Private Declare Function OemKeyScan Lib "user32" (ByVal wOemChar As Integer)
As _
Long
Private Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal _
lpszSrc As String, ByVal lpszDst As String) As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal
cChar _
As Byte) As Integer
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
(ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Type VKType
VKCode As Integer
scanCode As Integer
Control As Boolean
Shift As Boolean
Alt As Boolean
End Type
'---------------------------------------------------------------------
' Routine: VbSendKeys()
'
' Author: Bryan Wolf, 1999
'
' Purpose: Imitate VB's internal SendKeys statement, but add the
' ability to send keypresses to a DOS application. You
' can use SendKeys, to paste ASCII characters to a DOS
' window from the clipboard, but you can't send function
' keys. This module solves that problem and makes sending
' any keys to any Application, DOS or Windows, easy.
'
' Arguments: Keystrokes. Note that this does not implement the
' SendKeys's 'wait' argument. If you need to wait,
' try using a timing loop.
'
' The syntax for specifying keystrokes is the
' same as that of SendKeys - Please refer to VB's
' documentation for an in-depth description. Support
' for the following codes has been added, in addition
' to the standard set of codes suppored by SendKeys:
'
' KEY CODE
' break {CANCEL}
' escape {ESCAPE}
' left mouse button {LBUTTON}
' right mouse button {RBUTTON}
' middle mouse button {MBUTTON}
' clear {CLEAR}
' shift {SHIFT}
' control {CONTROL}
' alt {MENU} or {ALT}
' pause {PAUSE}
' space {SPACE}
' select {SELECT}
' execute {EXECUTE}
' snapshot {SNAPSHOT}
' number pad 0 {NUMPAD0}
' number pad 1 {NUMPAD1}
' number pad 2 {NUMPAD2}
' number pad 3 {NUMPAD3}
' number pad 4 {NUMPAD4}
' number pad 5 {NUMPAD5}
' number pad 6 {NUMPAD6}
' number pad 7 {NUMPAD7}
' number pad 8 {NUMPAD8}
' number pad 9 {NUMPAD9}
' number pad multiply {MULTIPLY}
' number pad add {ADD}
' number pad separator {SEPARATOR}
' number pad subtract {SUBTRACT}
' number pad decimal {DECIMAL}
' number pad divide {DIVIDE}
'
' Sample Calls:
' VbSendKeys "Dir~" ' View a directory of in DOS
'
' NOTE: there is a minor difference with SendKeys syntax. You can
' group multiple characters under the same shift key using
' curly brackets, while VB's SendKeys uses regular brackets.
' For example, to keep the SHIFT key pressed while you type
' A, B, and C keys, you must run the following statement
' VBSendKeys "+{abc}"
' while the syntax of the built-in VB function is
' SendKeys "+(abc)"
'---------------------------------------------------------------------
' Parse the string in the same way that SendKeys() would
Do While Len(sKeystrokes)
lRepetitions = 1 ' Default number of repetitions for each character
bShiftKey = False
bControlKey = False
bAltKey = False
Do While InStr(" ^%+", sKey) > 1 ' The space in " ^%+" is necessary
If sKey = "+" Then
bShiftKey = True
ElseIf sKey = "^" Then
bControlKey = True
ElseIf sKey = "%" Then
bAltKey = True
End If
sKey = Left$(sKeystrokes, 1)
sKeystrokes = Mid$(sKeystrokes, 2)
Loop
If bControlKey Then
keybd_event VirtualKeys(vbKeyControl).VKCode, _
VirtualKeys(vbKeyControl).scanCode, KEYEVENTF_KEYDOWN, 0
End If
If bAltKey Then
keybd_event VirtualKeys(vbKeyMenu).VKCode, _
VirtualKeys(vbKeyMenu).scanCode, KEYEVENTF_KEYDOWN, 0
End If
If bControlKey Then
keybd_event VirtualKeys(vbKeyControl).VKCode, _
VirtualKeys(vbKeyControl).scanCode, KEYEVENTF_KEYUP, 0
End If
If bAltKey Then
keybd_event VirtualKeys(vbKeyMenu).VKCode, _
VirtualKeys(vbKeyMenu).scanCode, KEYEVENTF_KEYUP, 0
End If
Bryan Wolf
SendKeys
SendKeys.GetCommands()
If you want to open the system menu of a notepad and Maximize screen. (Alt + Space)
Here is a complete list of key commands for 2005 with the 2.0 framework:
Mouse Functions
If you want to get the windows that have mouse focus. The main window and child under the
cursor.
SendKeys.Sleep(3000)
Dim w As SendKeys.WINFOCUS = SendKeys.GetWinFocus(True, False)
MessageBox.Show(w.Foreground.ToString & vbCrLf & w.Focus.ToString)
If you want to click the 12th menu item, by specifying the indexes only and no name.
If you want to wait for a window to exist before moving on, use the WaitForWindow function.
SendKeys.WaitForWindow("Print")
This module is built upon previous classes, such as InputToWin and SendKeysToWindow,
which were pretty reliable compared to SendKeys.Send but were still incomplete. The most
reliable methods for sending keys are Text and Message. Another dependable method is the new
Send method; it sends messages or uses a keyboard hook to ensure that the foreground window
is maintained, and only the keys sent from inside the module are allowed. It also can use the
AttachThreadInput API when the intended focus window is an external application. You now
can send keys directly to a child window by using the GetWinHandles or GetWinFocus(False)
methods, with the WINFOCUS structure. The SendKeys module is fully commented line by
line, to make it easier for others to learn and understand what's going on behind the scenes.