vb6.0 sendkeys function tutorial

21
Sending Keys You can easily send keys to the active window, as if the user was pressing keys on the keyboard by using the SendKeys statement. Shell "notepad", vbNormalFocus SendKeys "This is a test string" 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}

Upload: sandipon-sobhit-goswami

Post on 29-Mar-2015

2.710 views

Category:

Documents


38 download

TRANSCRIPT

Page 1: VB6.0 SendKeys Function Tutorial

Sending Keys

You can easily send keys to the active window, as if the user was pressing keys on the keyboard by using the SendKeys statement.

Shell "notepad", vbNormalFocus

SendKeys "This is a test string"

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}

Page 2: VB6.0 SendKeys Function Tutorial

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.

Page 3: VB6.0 SendKeys Function Tutorial

VBSendKeys - Send keys to any Windows and MS-DOS applicationPrivate Declare Function OemKeyScan Lib "user32" (ByVal wOemChar As Integer) As _ LongPrivate Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal _ lpszSrc As String, ByVal lpszDst As String) As LongPrivate Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar _ As Byte) As IntegerPrivate Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _ (ByVal wCode As Long, ByVal wMapType As Long) As LongPrivate Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _ ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYDOWN As Long = &H0Private Const KEYEVENTF_KEYUP As Long = &H2

Type VKType VKCode As Integer scanCode As Integer Control As Boolean Shift As Boolean Alt As BooleanEnd 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}

Page 4: VB6.0 SendKeys Function Tutorial

' 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)"'---------------------------------------------------------------------

Sub VbSendKeys(ByVal sKeystrokes As String) Dim iKeyStrokesLen As Integer Dim lRepetitions As Long Dim bShiftKey As Boolean Dim bControlKey As Boolean Dim bAltKey As Boolean Dim lResult As Long Dim sKey As String Dim iAsciiKey As Integer Dim iVirtualKey As Integer Dim i As Long Dim j As Long Static bInitialized As Boolean Static AsciiKeys(0 To 255) As VKType Static VirtualKeys(0 To 255) As VKType

Page 5: VB6.0 SendKeys Function Tutorial

On Error GoTo 0

If Not bInitialized Then Dim iKey As Integer Dim OEMChar As String Dim keyScan As Integer ' Initialize AsciiKeys() For iKey = LBound(AsciiKeys) To UBound(AsciiKeys) keyScan = VkKeyScan(iKey) AsciiKeys(iKey).VKCode = keyScan And &HFF ' low-byte of key scan ' code AsciiKeys(iKey).Shift = (keyScan And &H100) AsciiKeys(iKey).Control = (keyScan And &H200) AsciiKeys(iKey).Alt = (keyScan And &H400) ' Get the ScanCode OEMChar = " " ' 2 Char CharToOem Chr(iKey), OEMChar AsciiKeys(iKey).scanCode = OemKeyScan(Asc(OEMChar)) And &HFF Next iKey ' Initialize VirtualKeys() For iKey = LBound(VirtualKeys) To UBound(VirtualKeys) VirtualKeys(iKey).VKCode = iKey VirtualKeys(iKey).scanCode = MapVirtualKey(iKey, 0) ' no use in initializing remaining elements Next iKey bInitialized = True ' don't run this code twice End If ' End of initialization routine ' 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 ' Pull off Control, Alt or Shift specifiers sKey = Left$(sKeystrokes, 1) sKeystrokes = Mid$(sKeystrokes, 2) 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 ' Look for "{}" If sKey = "{" Then ' Look for the "}" i = InStr(sKeystrokes, "}")

Page 6: VB6.0 SendKeys Function Tutorial

If i > 0 Then sKey = Left$(sKeystrokes, i - 1) ' extract the content between ' the {} sKeystrokes = Mid$(sKeystrokes, i + 1) ' Remove the } End If ' Look for repetitions i = Len(sKey) Do While Mid$(sKey, i, 1) >= "0" And Mid$(sKey, i, _ 1) <= "9" And i >= 3 i = i - 1 Loop If i < Len(sKey) Then ' If any digits were found... If i >= 2 Then ' If there is something preceding it... If Mid$(sKey, i, 1) = " " Then ' If a space precedes the ' digits... On Error Resume Next ' On overflow, ignore the value lRepetitions = CLng(Mid$(sKey, i + 1)) On Error GoTo 0 sKey = Left$(sKey, i - 1) End If End If End If End If ' Look for special words Select Case UCase$(sKey) Case "LBUTTON" ' New iVirtualKey = vbKeyLButton Case "RBUTTON" ' New iVirtualKey = vbKeyRButton Case "BREAK", "CANCEL" iVirtualKey = vbKeyCancel Case "MBUTTON" ' New iVirtualKey = vbKeyMButton Case "BACKSPACE", "BS", "BKSP" iVirtualKey = vbKeyBack Case "TAB" iVirtualKey = vbKeyTab Case "CLEAR" ' New iVirtualKey = vbKeyClear Case "ENTER", "~" iVirtualKey = vbKeyReturn Case "SHIFT" ' New iVirtualKey = vbKeyShift Case "CONTROL" ' New iVirtualKey = vbKeyControl Case "MENU", "ALT" ' New iVirtualKey = vbKeyMenu Case "PAUSE" ' New iVirtualKey = vbKeyPause Case "CAPSLOCK" iVirtualKey = vbKeyCapital Case "ESCAPE", "ESC" iVirtualKey = vbKeyEscape Case "SPACE" ' New

Page 7: VB6.0 SendKeys Function Tutorial

iVirtualKey = vbKeySpace Case "PGUP" iVirtualKey = vbKeyPageUp Case "PGDN" iVirtualKey = vbKeyPageDown Case "END" iVirtualKey = vbKeyEnd Case "HOME" ' New iVirtualKey = vbKeyHome Case "LEFT" iVirtualKey = vbKeyLeft Case "UP" iVirtualKey = vbKeyUp Case "RIGHT" iVirtualKey = vbKeyRight Case "DOWN" iVirtualKey = vbKeyDown Case "SELECT" ' New iVirtualKey = vbKeySelect Case "PRTSC" iVirtualKey = vbKeyPrint Case "EXECUTE" ' New iVirtualKey = vbKeyExecute Case "SNAPSHOT" ' New iVirtualKey = vbKeySnapshot Case "INSERT", "INS" iVirtualKey = vbKeyInsert Case "DELETE", "DEL" iVirtualKey = vbKeyDelete Case "HELP" iVirtualKey = vbKeyHelp Case "NUMLOCK" iVirtualKey = vbKeyNumlock Case "SCROLLLOCK" iVirtualKey = vbKeyScrollLock Case "NUMPAD0" ' New iVirtualKey = vbKeyNumpad0 Case "NUMPAD1" ' New iVirtualKey = vbKeyNumpad1 Case "NUMPAD2" ' New iVirtualKey = vbKeyNumpad2 Case "NUMPAD3" ' New iVirtualKey = vbKeyNumpad3 Case "NUMPAD4" ' New iVirtualKey = vbKeyNumpad4 Case "NUMPAD5" ' New iVirtualKey = vbKeyNumpad5 Case "NUMPAD6" ' New iVirtualKey = vbKeyNumpad6 Case "NUMPAD7" ' New iVirtualKey = vbKeyNumpad7 Case "NUMPAD8" ' New iVirtualKey = vbKeyNumpad8 Case "NUMPAD9" ' New iVirtualKey = vbKeyNumpad9 Case "MULTIPLY" ' New iVirtualKey = vbKeyMultiply

Page 8: VB6.0 SendKeys Function Tutorial

Case "ADD" ' New iVirtualKey = vbKeyAdd Case "SEPARATOR" ' New iVirtualKey = vbKeySeparator Case "SUBTRACT" ' New iVirtualKey = vbKeySubtract Case "DECIMAL" ' New iVirtualKey = vbKeyDecimal Case "DIVIDE" ' New iVirtualKey = vbKeyDivide Case "F1" iVirtualKey = vbKeyF1 Case "F2" iVirtualKey = vbKeyF2 Case "F3" iVirtualKey = vbKeyF3 Case "F4" iVirtualKey = vbKeyF4 Case "F5" iVirtualKey = vbKeyF5 Case "F6" iVirtualKey = vbKeyF6 Case "F7" iVirtualKey = vbKeyF7 Case "F8" iVirtualKey = vbKeyF8 Case "F9" iVirtualKey = vbKeyF9 Case "F10" iVirtualKey = vbKeyF10 Case "F11" iVirtualKey = vbKeyF11 Case "F12" iVirtualKey = vbKeyF12 Case "F13" iVirtualKey = vbKeyF13 Case "F14" iVirtualKey = vbKeyF14 Case "F15" iVirtualKey = vbKeyF15 Case "F16" iVirtualKey = vbKeyF16 Case Else ' Not a virtual key iVirtualKey = -1 End Select ' Turn on CONTROL, ALT and SHIFT keys as needed If bShiftKey Then keybd_event VirtualKeys(vbKeyShift).VKCode, _ VirtualKeys(vbKeyShift).scanCode, KEYEVENTF_KEYDOWN, 0 End If If bControlKey Then keybd_event VirtualKeys(vbKeyControl).VKCode, _ VirtualKeys(vbKeyControl).scanCode, KEYEVENTF_KEYDOWN, 0 End If

Page 9: VB6.0 SendKeys Function Tutorial

If bAltKey Then keybd_event VirtualKeys(vbKeyMenu).VKCode, _ VirtualKeys(vbKeyMenu).scanCode, KEYEVENTF_KEYDOWN, 0 End If ' Send the keystrokes For i = 1 To lRepetitions If iVirtualKey > -1 Then ' Virtual key keybd_event VirtualKeys(iVirtualKey).VKCode, _ VirtualKeys(iVirtualKey).scanCode, KEYEVENTF_KEYDOWN, 0 keybd_event VirtualKeys(iVirtualKey).VKCode, _ VirtualKeys(iVirtualKey).scanCode, KEYEVENTF_KEYUP, 0 Else ' ASCII Keys For j = 1 To Len(sKey) iAsciiKey = Asc(Mid$(sKey, j, 1)) ' Turn on CONTROL, ALT and SHIFT keys as needed If Not bShiftKey Then If AsciiKeys(iAsciiKey).Shift Then keybd_event VirtualKeys(vbKeyShift).VKCode, _ VirtualKeys(vbKeyShift).scanCode, _ KEYEVENTF_KEYDOWN, 0 End If End If If Not bControlKey Then If AsciiKeys(iAsciiKey).Control Then keybd_event VirtualKeys(vbKeyControl).VKCode, _ VirtualKeys(vbKeyControl).scanCode, _ KEYEVENTF_KEYDOWN, 0 End If End If If Not bAltKey Then If AsciiKeys(iAsciiKey).Alt Then keybd_event VirtualKeys(vbKeyMenu).VKCode, _ VirtualKeys(vbKeyMenu).scanCode, _ KEYEVENTF_KEYDOWN, 0 End If End If ' Press the key keybd_event AsciiKeys(iAsciiKey).VKCode, _ AsciiKeys(iAsciiKey).scanCode, KEYEVENTF_KEYDOWN, 0 keybd_event AsciiKeys(iAsciiKey).VKCode, _ AsciiKeys(iAsciiKey).scanCode, KEYEVENTF_KEYUP, 0 ' Turn on CONTROL, ALT and SHIFT keys as needed If Not bShiftKey Then If AsciiKeys(iAsciiKey).Shift Then keybd_event VirtualKeys(vbKeyShift).VKCode, _ VirtualKeys(vbKeyShift).scanCode, _ KEYEVENTF_KEYUP, 0 End If End If

Page 10: VB6.0 SendKeys Function Tutorial

If Not bControlKey Then If AsciiKeys(iAsciiKey).Control Then keybd_event VirtualKeys(vbKeyControl).VKCode, _ VirtualKeys(vbKeyControl).scanCode, _ KEYEVENTF_KEYUP, 0 End If End If If Not bAltKey Then If AsciiKeys(iAsciiKey).Alt Then keybd_event VirtualKeys(vbKeyMenu).VKCode, _ VirtualKeys(vbKeyMenu).scanCode, _ KEYEVENTF_KEYUP, 0 End If End If Next j ' Each ASCII key End If ' ASCII keys Next i ' Repetitions ' Turn off CONTROL, ALT and SHIFT keys as needed If bShiftKey Then keybd_event VirtualKeys(vbKeyShift).VKCode, _ VirtualKeys(vbKeyShift).scanCode, KEYEVENTF_KEYUP, 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 Loop ' sKeyStrokesEnd Sub

Bryan Wolf

Page 11: VB6.0 SendKeys Function Tutorial

SendKeys

If you want set focus without sending keys.

SendKeys.Send("", SendKeys.GetWinHandles("New Text Document - _ Notepad", 1, "Edit", 1), False)

If you want to get the number of keys sent.

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("New Text Document - Notepad", 1, "Edit", 1)Dim sent As Int32 = SendKeys.Send("Hello", w)MessageBox.Show(sent.ToString & " keys sent")

If you want to view available commands.

SendKeys.GetCommands()

If you want to open a file menu. (Alt + f)

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("New Text Document - Notepad")SendKeys.Send("%(f)", w, False)

If you want to access sub menu shortcuts. (Alt + fx)

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("New Text Document - Notepad")SendKeys.Send("%(fx)", w, False)

If you want capitalize a letter. (Shift + a)

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("New Text Document - Notepad")SendKeys.Send("+(a)", w, False)

If you want print the current document. (Ctrl + p)

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("New Text Document - Notepad")SendKeys.Send("^(p)", w, False)

If you want to open the system menu of a notepad and Maximize screen. (Alt + Space)

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("New Text Document - Notepad")SendKeys.Send("%({space}x)", w, False)

Page 12: VB6.0 SendKeys Function Tutorial

If you want to open start menu. (Ctrl + Escape)

SendKeys.Send("^({escape})", Nothing, False)

If you want to open Task manager. (Ctrl+Shift+Escape)

SendKeys.Send("^(+({escape}))", Nothing, False)

If you want to log off of 2000/XP immediately. (Win + L)SendKeys.Send("#(l)", Nothing, False)

Important Note: Different versions of VB.NET have different names for certain keys. For example, the {enter} key on VB.NET 2003 is now the {return} key on VB.NET 2005. I recommend checking what the key names are for your version of VB.NET, by using the GetCommands sub.

Here is a complete list of key commands for 2005 with the 2.0 framework:

KeyCode Command KeyCode Command KeyCode Command

0 None 85 U 171 BrowserFavorites

1 LButton 86 V 172 BrowserHome

2 RButton 87 W 173 VolumeMute

3 Cancel 88 X 174 VolumeDown

4 MButton 89 Y 175 VolumeUp

5 XButton1 90 Z 176 MediaNextTrack

6 XButton2 91 LWin 177 MediaPreviousTrack

7 LButton, XButton2

92 RWin 178 MediaStop

8 Back 93 Apps 179 MediaPlayPause

9 Tab 94 RButton, RWin 180 LaunchMail

10 LineFeed 95 Sleep 181 SelectMedia

11 LButton, LineFeed 96 NumPad0 182 LaunchApplication1

12 Clear 97 NumPad1 183 LaunchApplication2

13 Return 98 NumPad2 184 Back, MediaNextTrack

14 RButton, Clear 99 NumPad3 185 Back, MediaPreviousTrack

15 RButton, Return 100 NumPad4 186 Oem1

16 ShiftKey 101 NumPad5 187 Oemplus

17 ControlKey 102 NumPad6 188 Oemcomma

18 Menu 103 NumPad7 189 OemMinus

Page 13: VB6.0 SendKeys Function Tutorial

19 Pause 104 NumPad8 190 OemPeriod

20 Capital 105 NumPad9 191 OemQuestion

21 KanaMode 106 Multiply 192 Oemtilde

22 RButton, Capital 107 Add 193 LButton, Oemtilde

23 JunjaMode 108 Separator 194 RButton, Oemtilde

24 FinalMode 109 Subtract 195 Cancel, Oemtilde

25 HanjaMode 110 Decimal 196 MButton, Oemtilde

26 RButton, FinalMode

111 Divide 197 XButton1, Oemtilde

27 Escape 112 F1 198 XButton2, Oemtilde

28 IMEConvert 113 F2 199 LButton, XButton2, Oemtilde

29 IMENonconvert 114 F3 200 Back, Oemtilde

30 IMEAceept 115 F4 201 Tab, Oemtilde

31 IMEModeChange 116 F5 202 LineFeed, Oemtilde

32 Space 117 F6 203 LButton, LineFeed, Oemtilde

33 PageUp 118 F7 204 Clear, Oemtilde

34 Next 119 F8 205 Return, Oemtilde

35 End 120 F9 206 RButton, Clear, Oemtilde

36 Home 121 F10 207 RButton, Return, Oemtilde

37 Left 122 F11 208 ShiftKey, Oemtilde

38 Up 123 F12 209 ControlKey, Oemtilde

39 Right 124 F13 210 Menu, Oemtilde

40 Down 125 F14 211 Pause, Oemtilde

41 Select 126 F15 212 Capital, Oemtilde

42 Print 127 F16 213 KanaMode, Oemtilde

43 Execute 128 F17 214 RButton, Capital, Oemtilde

44 PrintScreen 129 F18 215 JunjaMode, Oemtilde

45 Insert 130 F19 216 FinalMode, Oemtilde

46 Delete 131 F20 217 HanjaMode, Oemtilde

Page 14: VB6.0 SendKeys Function Tutorial

47 Help 132 F21 218 RButton, FinalMode, Oemtilde

48 D0 133 F22 219 OemOpenBrackets

49 D1 134 F23 220 Oem5

50 D2 135 F24 221 Oem6

51 D3 136 Back, F17 222 Oem7

52 D4 137 Back, F18 223 Oem8

53 D5 138 Back, F19 224 Space, Oemtilde

54 D6 139 Back, F20 225 PageUp, Oemtilde

55 D7 140 Back, F21 226 OemBackslash

56 D8 141 Back, F22 227 LButton, OemBackslash

57 D9 142 Back, F23 228 Home, Oemtilde

58 RButton, D8 143 Back, F24 229 ProcessKey

59 RButton, D9 144 NumLock 230 MButton, OemBackslash

60 MButton, D8 145 Scroll 231 Packet

61 MButton, D9 146 RButton, NumLock 232 Down, Oemtilde

62 XButton2, D8 147 RButton, Scroll 233 Select, Oemtilde

63 XButton2, D9 148 MButton, NumLock

234 Back, OemBackslash

64 64 149 MButton, Scroll 235 Tab, OemBackslash

65 A 150 XButton2, NumLock

236 PrintScreen, Oemtilde

66 B 151 XButton2, Scroll 237 Back, ProcessKey

67 C 152 Back, NumLock 238 Clear, OemBackslash

68 D 153 Back, Scroll 239 Back, Packet

69 E 154 LineFeed, NumLock

240 D0, Oemtilde

70 F 155 LineFeed, Scroll 241 D1, Oemtilde

71 G 156 Clear, NumLock 242 ShiftKey, OemBackslash

72 H 157 Clear, Scroll 243 ControlKey, OemBackslash

73 I 158 RButton, Clear, 244 D4, Oemtilde

Page 15: VB6.0 SendKeys Function Tutorial

NumLock

74 J 159 RButton, Clear, Scroll

245 ShiftKey, ProcessKey

75 K 160 LShiftKey 246 Attn

76 L 161 RShiftKey 247 Crsel

77 M 162 LControlKey 248 Exsel

78 N 163 RControlKey 249 EraseEof

79 O 164 LMenu 250 Play

80 P 165 RMenu 251 Zoom

81 Q 166 BrowserBack 252 NoName

82 R 167 BrowserForward 253 Pa1

83 S 168 BrowserRefresh 254 OemClear

84 T 169 BrowserStop 255 LButton, OemClear

    170 BrowserSearch    

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 a button or other window.

Dim w As New SendKeys.WINFOCUSw = SendKeys.GetWinHandles("Form1", 1, "GroupBox1", 1, "GroupBox2", _ 1, "GroupBox3", 1, "Start", 1)SendKeys.Click(Buttons.LeftClick, w)

If you want to move from current cursor position.

SendKeys.Click(Buttons.Move, Nothing, False, False, True, 100, 100)

Page 16: VB6.0 SendKeys Function Tutorial

If you want to move the cursor to screen coordinates.

SendKeys.Click(Buttons.MoveAbsolute, Nothing, False, False, True, _ 100, 100)

If you want to click the 12th menu item, as a command message.

SendKeys.ClickMenu(True, "Form1", 1, "File", 1, "Item2", 1, "Item6", _ 1, "Item9", 1, "Item10", 1, "Item12", 1)

If you want to click the 23rd menu item.

SendKeys.ClickMenu(True, "Form1", 1, "Edit", 1, "Item15", 1, _ "Item21", 1, "Item23", 1)

If you want to click the 12th menu item, by specifying the indexes only and no name.

SendKeys.ClickMenu(True, "Form1", 1, "", 1, "", 1, "", 3, "", 2, _ "", 1, "", 2)

If you want to click an item by specifying both captions and indexes.

SendKeys.ClickMenu(True, "Form1", 1, "Edit", 1, "Item15", 1, _ "Item18", 2)

If you want to wait for a window to exist before moving on, use the WaitForWindow function.

SendKeys.WaitForWindow("Print")

What's Under the Hood?

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.

Advantages of the SendKeys Module over the SendKeys Class

Keyboard focus is maintained on the application by using a keyboard hook and SetForegroundWindow. When keys are sent from this module, external keys entered by the user are ignored by the hook. Other applications are locked out from using the SetForegroundWindow function while keys are being sent. The foregroundwindow can be returned to its original position. When a key being pressed represents an action that starts a new window, you can wait

Page 17: VB6.0 SendKeys Function Tutorial

until that window exists before sending more keys. Win+key combos are now possible (2000/Xp) and are expressed as a string: "#(l)" (Win+L). All keys between 0 and 255 can be called! See the GetCommands sub for all available commands. GetWinHandles can find and verify any window handle, or start an executable path. See the GetCommands sub. The Sleep method provides a unique way to overcome timing issues. Complex key combinations are possible. Keys can be sent to a disabled, minimized, or hidden window. Strings can be repeated; in other words, "{hello 10}" repeats the hello string ten times.

Disadvantages of the SendKeys Module Compared to the SendKeys Class

None yet known. Let me know if you find any!

Author name: Shane Findley