Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > API and Miscellaneous Code Examples

Replacement for Sendkeys using a class containing API calls

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
Replacement for Sendkeys using a class containing API calls The following class module code contains code which can be used as a replacement for the "SendKeys" statement. 'Place following code in a class module Option Explicit Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) 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) Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Any) As Long Private Const KEYEVENTF_EXTENDEDKEY = &H1 Private Const KEYEVENTF_KEYUP = &H2 Public Enum eKeys keyBackspace = &H8 keyTab = &H9 keyReturn = &HD keyShift = &H10 keyControl = &H11 keyAlt = &H12 keyPause = &H13 keyEscape = &H1B keySpace = &H20 keyEnd = &H23 keyHome = &H24 keyLeft = &H25 KeyUp = &H26 keyRight = &H27 KeyDown = &H28 keyInsert = &H2D keyDelete = &H2E keyF1 = &H70 keyF2 = &H71 keyF3 = &H72 keyF4 = &H73 keyF5 = &H74 keyF6 = &H75 keyF7 = &H76 keyF8 = &H77 keyF9 = &H78 keyF10 = &H79 keyF11 = &H7A keyF12 = &H7B keyNumLock = &H90 keyScrollLock = &H91 keyCapsLock = &H14 End Enum Public Enum eLockKey CapsLock = keyCapsLock NumLock = keyNumLock ScrollLock = keyScrollLock End Enum Private zlKeyPressDelay As Long 'Purpose : Press a single key represented by a string 'Inputs : sKey The key to press. ' [bHoldKeydown] If True does not release the key press. ' [bRelease] If True releases a "HoldKeydown" key press (see above). 'Outputs : N/A Public Sub SendKey(sKey As String, Optional bHoldKeydown As Boolean = False, Optional bRelease As Boolean = False) Dim lScan As Long, lExtended As Long, lVK As Long Dim bShift As Boolean, bCtrl As Boolean, bAlt As Boolean lVK = VkKeyScan(Asc(sKey)) If lVK Then lScan = MapVirtualKey(lVK, 2) lExtended = 0 If lScan = 0 Then lExtended = KEYEVENTF_EXTENDEDKEY End If lScan = MapVirtualKey(lVK, 0) bShift = (lVK And &H100) bCtrl = (lVK And &H200) bAlt = (lVK And &H400) lVK = (lVK And &HFF) If bRelease = False Then If bShift Then keybd_event eKeys.keyShift, 0, 0, 0 End If If bCtrl Then keybd_event eKeys.keyControl, 0, 0, 0 End If If bAlt Then keybd_event eKeys.keyAlt, 0, 0, 0 End If keybd_event lVK, lScan, lExtended, 0 End If If bHoldKeydown = False Then keybd_event lVK, lScan, KEYEVENTF_KEYUP Or lExtended, 0 If bShift Then keybd_event eKeys.keyShift, 0, KEYEVENTF_KEYUP, 0 End If If bCtrl Then keybd_event eKeys.keyControl, 0, KEYEVENTF_KEYUP, 0 End If If bAlt Then keybd_event eKeys.keyAlt, 0, KEYEVENTF_KEYUP, 0 End If End If End If End Sub 'Purpose : Loops through a string and calls SendKey for each character 'Inputs : sString The string to press. ' [bDoEvents] If True returns control to the processor after every keypress. 'Outputs : N/A Public Sub SendString(ByVal sString As String, Optional bDoEvents As Boolean = True) Dim sKey As String * 1, lKey As Long, lLen As Long lLen = Len(sString) For lKey = 1 To lLen sKey = Mid$(sString, lKey, 1) SendKey sKey Sleep zlKeyPressDelay If bDoEvents Then DoEvents End If Next End Sub 'Purpose : Presses a virtual key (used for keys that don't have ascii equilivant) 'Inputs : ekeyPress The virtual key to press ' [bHoldKeydown] If True does not release the key press. ' [bRelease] If True releases the key press. ' [bCompatible] 'Outputs : N/A Public Sub PressKeyVK(ekeyPress As eKeys, Optional bHoldKeydown As Boolean, Optional bRelease As Boolean, Optional bCompatible As Boolean) Dim lScan As Long Dim lExtended As Long lScan = MapVirtualKey(ekeyPress, 2) lExtended = 0 If lScan = 0 Then lExtended = KEYEVENTF_EXTENDEDKEY End If lScan = MapVirtualKey(ekeyPress, 0) If bCompatible Then lExtended = 0 End If If Not bRelease Then keybd_event ekeyPress, lScan, lExtended, 0 End If If Not bHoldKeydown Then keybd_event ekeyPress, lScan, KEYEVENTF_KEYUP Or lExtended, 0 End If End Sub 'Purpose : Sets the status of the various Lock keys 'Inputs : LockKey The lock key to set the status of. 'Outputs : N/A Public Sub SetLockStatus(LockKey As eLockKey, bOn As Boolean) Dim abKeys(0 To 256) As Byte abKeys(LockKey) = (bOn * -1) 'Win 9x Call SetKeyboardState(abKeys(0)) If GetKeyState(LockKey) <> IIf(bOn, 0, 1) Then 'GetKeyState returns 1 if OFF and 0 if ON 'Win NT 'Simulate Key Press keybd_event LockKey, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0 'Simulate Key Release keybd_event LockKey, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0 End If End Sub 'Purpose : returns the status of the various Lock keys 'Inputs : LockKey The lock key to return the status of. 'Outputs : Returns True if the Lock status is on. Public Function GetLockStatus(LockKey As eLockKey) As Boolean GetLockStatus = GetKeyState(LockKey) End Function 'Determines the delay between subsequent keystrokes in the SendString routine Property Get KeyPressDelay() As Long KeyPressDelay = zlKeyPressDelay End Property Property Let KeyPressDelay(Value As Long) zlKeyPressDelay = Value End Property Private Sub Class_Initialize() zlKeyPressDelay = 20 'Add a delay of 20 ms between pressing each key End Sub