Solved

What is the difference.......

Posted on 2000-02-19
13
311 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
Comment Utility
>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
Comment Utility
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 150 total points
Comment Utility
' 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
Comment Utility
Adjusted points to 150
0
 
LVL 3

Author Comment

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

Expert Comment

by:ameba
Comment Utility
Thanks for the points!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now