• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1259
  • Last Modified:

VB: Postmessage, Shift and control

I tied to use the postmessage command to send keystrokes to MS Word 97 (I'm using Word 97 as a test of my program).  I used the WM_KEYDOWN, WM_CHAR, and WM_KEYUP commands.

(I'm not creating a process instance, I'm just grabbing the hwnd of an already open app.)

The order of messages are:
1.  WM_KEYDOWN shift
3.  WM_CHAR "A"
4.  WM_KEYUP "A"

What happens is that a capital A and a lowercase a is sent.  When I remove the WM_CHAR, only one a is sent (lower case).  When I put the caps lock on, then a capital A is sent.  All of the values are &H41 (65 , letter A).

My suspiscion is that when I send the shift message, it doesn't "hold" while the next message is being sent, ie, when I send the a, it checks that status of the shift key.

I'm not sure if this is the way MS Word handles keystrokes, but I need to be able to send keystrokes this way (not via Sendkeys or COM or any way.  I WANT to use the post message way).

My question is, how do I force Shift and letter combinations, and especially, how do I force Control and letter combinations vi postmessage  (send message won't work)?  Do I have to force a caps locks?  What about CTRL, since there is no CTRL lock?  

There are some apps besides Word that I need to send control combinations (ie CTRL + Z)

  • 2
  • 2
1 Solution
why don't you use SendMessage?

obregoruAuthor Commented:

This seems hard, even for the C++ forum.
I tried using the keybd_event (I'm using NT), a mix of keybd_event, or even all post messages no luck.

VB is reporting the shift being pressed, but it is not functioning in MS word.  I'm still getting lower case a's.


 Call keybd_event(&H10, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
            Call PostMessage(&H105D4, WM_KEYDOWN, &H41, &H1E0001)  'A down
            Call PostMessage(&H105D4, &H101, &H41, &HC01E0001) 'A up
            Call keybd_event(&H10, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
here is an example how to do it,....


  dim h as long

  h = FindWindow(vbNullString, "Document - WordPad")
  if h = 0 then
    MsgBox "Error: Invalid Handle"
    Exit Sub
  end if
  call SetFocus(h)
  call SetForegroundWindow(h)

  //lower case letter
  call keybd_event(65, 0, 0, 0);
  call keybd_event(65, 0, KEYEVENTF_KEYUP, 0)

  //upper case letter
  call keybd_event(vk_shift, 0, KEYEVENTF_EXTENDEDKEY, 0)
  call keybd_event(65, 0, 0, 0)
  call keybd_event(65, 0, KEYEVENTF_KEYUP, 0)
  call keybd_event(vk_shift, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,0)

Hope this helps..

obregoruAuthor Commented:
Thanks for the answer.  I did more work than I wanted to (I had to find SetFocus...), but it got the job done!

Here is the code I used:

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 FindWindow Lib "user32" Alias _
                                "FindWindowA" (ByVal lpClassName As String, _
                                ByVal lpWindowName As String) As Long

Private Declare Function SetFocusX Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
'setfocus is a VB reserved word, so we give it a new name

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

    Const VK_SHIFT = &H10

Dim h As Long

         h = FindWindow(vbNullString, "Microsoft Word - Document2")
         If h = 0 Then
           MsgBox "Error: Invalid Handle"
           Exit Sub
         End If

         Call SetFocusX(h)
         Call SetForegroundWindow(h)
         Call keybd_event(&H10, 0, KEYEVENTF_EXTENDEDKEY, 0)
         Call keybd_event(65, 0, 0, 0)
         Call keybd_event(65, 0, KEYEVENTF_KEYUP, 0)
         Call keybd_event(&H10, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)

Well if it works why did I get a B mark instead of an A?
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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now