What is the difference.......

What is the difference between the following:  These all send an Enter key

1.)  keybd_event vbKeyReturn, 0, 0, 0

2.)  keybd_event 13, 0, 0, 0

3.)  Public Const VK_RETURN = &HD
     keybd_event VK_RETURN, 0, 0, 0

I also know that you can use SENDKEYS to do this as well....what is the difference between using each one?

Who is Participating?
amebaConnect With a Mentor Commented:
' module code -------------
Option Explicit
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_PREV = &H21  'page up
Public Const VK_NEXT = &H22
Public Const VK_LEFT = &H25
Public Const VK_DOWN = &H28  ' down
Public Const KEYEVENTF_KEYUP = &H2
Public Const VK_SHIFT = &H10
Public Declare Function OemKeyScan Lib "user32" (ByVal wOemChar As Integer) As Long
Public Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
Public Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
    ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Sub SendKys(ByVal keys As String, WinCaption As String)
    Dim x&, t As Integer
    'loop thru string to send one key at a time
    For x& = 1 To Len(keys)
    'activate target application
        AppActivate WinCaption, 0
        'send one key to target
        SendAKey Mid$(keys, x&, 1)
    Next x&
End Sub

' send single character
Public Sub SendAKey(ByVal key As String)
    Dim vk%
    Dim shiftscan%
    Dim scan%
    Dim oemchar$
    Dim dl&
    Dim shiftkey%
    ' Get the virtual key code for this character
    vk% = VkKeyScan(Asc(key)) And &HFF
    ' See if shift key needs to be pressed
    shiftkey% = VkKeyScan(Asc(key)) And 256
    oemchar$ = " " ' 2 character buffer
    ' Get the OEM character - preinitialize the buffer
    CharToOem Left$(key, 1), oemchar$
    ' Get the scan code for this key
    scan% = OemKeyScan(Asc(oemchar$)) And &HFF
    ' Send the key down
    If shiftkey% = 256 Then
        'if shift key needs to be pressed
        shiftscan% = MapVirtualKey(VK_SHIFT, 0)
        'press down the shift key
        keybd_event VK_SHIFT, shiftscan%, 0, 0
    End If
    'press key to be sent
    keybd_event vk%, scan%, 0, 0
    ' Send the key up
    If shiftkey% = 256 Then
        'keyup for shift key
        keybd_event VK_SHIFT, shiftscan%, KEYEVENTF_KEYUP, 0
    End If
    'keyup for key sent
    keybd_event vk%, scan%, KEYEVENTF_KEYUP, 0
End Sub

' form code -------------
' (I use module to send keys to DOS app, but the sample is with notepad)
Option Explicit

Private Sub Form_Click()
    Dim WindowCaption As String, shiftscan%, i As Integer
    On Error Resume Next
    WindowCaption = "Untitled - Notepad"
    ' start notepad
    Shell "notepad.exe", vbNormalFocus
    ' send some text
    SendKys "Groone was here!" & vbCrLf & vbCrLf, WindowCaption
    ' press F5 to insert time
    shiftscan% = MapVirtualKey(VK_F5, 0)
    AppActivate WindowCaption, 0
    keybd_event VK_F5, shiftscan%, 0, 0
    keybd_event VK_F5, shiftscan%, KEYEVENTF_KEYUP, 0
    ' press left arrow 4 times
    For i = 1 To 4
        shiftscan% = MapVirtualKey(VK_LEFT, 0)
        AppActivate WindowCaption, 0
        keybd_event VK_LEFT, shiftscan%, 0, 0
        keybd_event VK_LEFT, shiftscan%, KEYEVENTF_KEYUP, 0
End Sub
The difference is simple although i'll give you a more complete answer later. IT allows you flexibility you are not tied to one answer.
Ok Keybd_event is an API which is fires that event it can accept parameters such as 13 which is an ascii cherecter for enter or it takes a vb constant vk_return. SendKeys also can do that.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

In programming, you can do things in different ways. For example,

For X=1 to 10

is the same as,

Loop Until X=10

There might be differences and there might not.
The difference is in speed and memory usage:
1.)  keybd_event vbKeyReturn, 0, 0, 0
vbKeyReturn does not use extra memory (it is defined in vb dll)
2.)  keybd_event 13, 0, 0, 0
13 will use module level memory (this is not loaded when module is not in use)
3.)  Public Const VK_RETURN = &HD
VK_RETURN will use global memory (app level table of constants)

keybd_event expects Byte as parameter. For 1, 2 and 3 compiler will add CByte function to convert to Byte: vbKeyReturn is Long, 13 and VK_RETURN are Integers.

CByte will also add some range checking.

4) In procedure where keybd_event is used, add:
Const VK_RETURN As Byte = &HD
grooneAuthor Commented:
so either one is as good as the other?  I've noticed that

keybd_event 13, 0, 0, 0

doesnt work in most Office products while

keybd_event vbKeyReturn, 0, 0, 0

does work in most office products.

So would the suggestion be to declare your key or to rely on vb ide to do it for you?  (i guess i wrote thatright)
grooneAuthor Commented:
Oh one more thing -

keybd_event 13, 0, 0, 0
is represented as a keydown and this
keybd_event 13, 0, 2, 0
is represented as a keyup

keybd_event vbKeyReturn, 0, 0, 0
is both key up and down or is it?

so when using vbkeys the key up no longer exists?
keybd_event vbKeyReturn, 0, 2, 0

I dont know why i started thinking about it....i guess it's becasue I was curiosu as to why some send keys wouldnt work in word unless I used vbkey  **I dunno**
My suggestion is 4.)
But I think 1, 2, 3 should also work without problem.

In VB7, there will be Option Strict, which will not allow 1, 2 and 3 to run - you will have to add explicit conversion CByte(13) or CByte(...) to use that very good new option.
>keybd_event 13, 0, 0, 0
>is represented as a keydown and this
>keybd_event 13, 0, 2, 0
>is represented as a keyup

Yes, you need both lines to press and then depress a key.
Note that this won't work 100% in Win2000, there are some new Input functions replacing keybd_event and mouse_event
grooneAuthor Commented:
For a few extra points Ameba, what would you use if you wished to send keystrokes across the board?  Would you use something different then one of these examples?
grooneAuthor Commented:
Adjusted points to 150
grooneAuthor Commented:
Thanks Ameba!  Once again you have cleared some fog!
Thanks for the points!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.