Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is the difference.......

Posted on 2000-02-19
13
Medium Priority
?
376 Views
Last Modified: 2008-02-01
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?

Thanks!
0
Comment
Question by:groone
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2539053
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.
0
 
LVL 6

Expert Comment

by:Marine
ID: 2539058
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.
0
 
LVL 7

Expert Comment

by:kamall
ID: 2539062
In programming, you can do things in different ways. For example,

For X=1 to 10
....
Next

is the same as,

Do
X=X+1
Loop Until X=10

There might be differences and there might not.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:ameba
ID: 2539213
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.

Suggestion:
4) In procedure where keybd_event is used, add:
Const VK_RETURN As Byte = &HD
0
 
LVL 3

Author Comment

by:groone
ID: 2539365
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)
0
 
LVL 3

Author Comment

by:groone
ID: 2539372
Oh one more thing -

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

now
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**
0
 
LVL 15

Expert Comment

by:ameba
ID: 2539374
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.
0
 
LVL 15

Expert Comment

by:ameba
ID: 2539396
>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
0
 
LVL 3

Author Comment

by:groone
ID: 2542880
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?
0
 
LVL 15

Accepted Solution

by:
ameba earned 600 total points
ID: 2543140
' 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_EXTENDEDKEY = &H1
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)
        DoEvents
    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
    DoEvents
    DoEvents
    DoEvents
    ' send some text
    SendKys "Groone was here!" & vbCrLf & vbCrLf, WindowCaption
    DoEvents
    ' 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
    DoEvents
    ' 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
        DoEvents
    Next
End Sub
0
 
LVL 3

Author Comment

by:groone
ID: 2544391
Adjusted points to 150
0
 
LVL 3

Author Comment

by:groone
ID: 2544392
Thanks Ameba!  Once again you have cleared some fog!
0
 
LVL 15

Expert Comment

by:ameba
ID: 2544406
Thanks for the points!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question