VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



The Better SendKeys Statement

by Ronald Borla (5 Submissions)
Category: VB function enhancement
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

The SendKeys statement is limited only to some keyboard presses...
While this code gives you more...
Such as:
1. You cannot send Space Keystrokes in SendKeys (I tried to do it, or maybe I just don't know how), while you can in HitKey...
HitKey Asc(" ") or
HitKey KeyCodeConstants.vbKeySpace
2. HitKey can also perform CapsLock, ScrollLock, NumLock, or other Lock, and other Keys you can find in your keyboard.. Remember to use VB Constants in "KeyCodeConstants"
code by: Ronald Borla

Rate The Better SendKeys Statement

Option Explicit
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 Function MapVirtualKey _
  Lib "user32" Alias "MapVirtualKeyA" _
  (ByVal wCode As Long, ByVal _
  wMapType As Long) As Long
 
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Public Sub HitKey(ByVal KeyCode As Integer)
'Send the key down
Call keybd_event(KeyCode, _
   MapVirtualKey(KeyCode, 0), _
   KEYEVENTF_EXTENDEDKEY Or 0, 0)
'Send the key up        
Call keybd_event(KeyCode, _
   MapVirtualKey(KeyCode, 0), _
   KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
DoEvents
End Sub
'###############################################
'Example for the use of the code:
'In you Timer Event
Private Sub tmrKeyPress_Timer()
'Sends Key Spaces every timer event
HitKey KeyCodeConstants.vbKeySpace
End Sub
'Use the VB Default KeyCodeConstants for easier
'use for this code.
'###############################################

Download this snippet    Add to My Saved Code

The Better SendKeys Statement Comments

No comments have been posted about The Better SendKeys Statement. Why not be the first to post a comment about The Better SendKeys Statement.

Post your comment

Subject:
Message:
0/1000 characters