Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


VB: Postmessage, Shift and control

Posted on 1999-07-22
Medium Priority
Last Modified: 2008-03-17
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)

Question by:obregoru
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 1526703
why don't you use SendMessage?


Author Comment

ID: 1526704

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)
LVL 10

Accepted Solution

viktornet earned 1500 total points
ID: 1526705
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..


Author Comment

ID: 1526706
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)

LVL 10

Expert Comment

ID: 1526707
Well if it works why did I get a B mark instead of an A?

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

670 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