Solved

How to implement SendKeys in C/C++

Posted on 1997-04-04
3
3,852 Views
Last Modified: 2013-12-03
I'm developing a 16-bit Windows app.  I did a prototype in Visual Basic and used the SendKeys() function to send keystrokes to other applications.  Now I'm writing the real product using Visual C++ V1.52 and can't figure out how to do the SendKeys.  I've tried sending WM_CHAR, WM_KEYUP, WM_KEYDOWN messages without luck.

In particular, I'm putting a metafile on the clipboard and trying to paste it into another application.  In the prototype I just called SendKeys to send the Ctrl-Insert keystroke and it worked like a charm.

Can anyone please help me out????
0
Comment
Question by:fitzy
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 300 total points
ID: 1334402
It should work. How do you get the window handle to which you would like to send the messages? Please note you should get the window handle that exactly processes key messages. For example, the edit window processes key messages instead of the main window does. The following tested code shows how to send the messages to Notepad.

    CWnd *pWnd = CWnd::FindWindow(NULL, "Untitled - Notepad");
    if (pWnd != NULL)
    {
        CWnd *pWndEdit = pWnd->GetWindow(GW_CHILD);
        if (pWndEdit != NULL)
        {
            pWndEdit->PostMessage(WM_KEYDOWN, VK_SPACE, 0x00390001UL);
            pWndEdit->PostMessage(WM_KEYUP, VK_SPACE, 0xC0390001UL);
            // or pWndEdit->PostMessage(WM_CHAR, VK_SPACE, 0x00390001UL);
           
            ::MessageBeep(-1);
        }
    }

0
 

Author Comment

by:fitzy
ID: 1334403
Recommending the call to GetWindow(GW_CHILD) was a good one and it did work with Notepad.  Unfortunately it does not work with other apps, though (e.g., Wordpad).  My  VB prototype  worked with everything I tried.  In the VB prototype I called
        AppActivate
then
        SendKeys

I need to make my app work with any destination application.  Do you have any recommendations?

Thanks!



0
 
LVL 23

Expert Comment

by:chensu
ID: 1334404
I see. Since you need to make your app work with any destination application, it is not so easy. You can do something like the following code,

    CWnd *pWnd = CWnd::FindWindow(NULL, "Document - Wordpad");
    if (pWnd != NULL)
    {
        pWnd->SendMessageToDescendants(WM_CHAR, VK_SPACE, 0x00390001UL);
       
        ::MessageBeep(-1);
    }

Let me explain further. Because SendMessageToDescendants uses SendMessage instead of PostMessage and SendMessage(WM_KEYDOWN), SendMessage(WM_KEYDOWN) do not generate WM_CHAR message, I use WM_CHAR directly. You can choose appropriate method depending on your program. You can also refer to MFC source code to see how SendMessageToDescendants works. You might want to write your own PostMessageToDescendants.

Initially, I thought the following code was the best way.

    CWnd *pWnd = CWnd::FindWindow(NULL, "Document - Wordpad");
    if (pWnd != NULL)
    {
        pWnd->SetActiveWindow();
       
        CWnd *pWndEdit = CWnd::GetFocus();
       
        if (pWndEdit != NULL)
        {
            pWndEdit->PostMessage(WM_KEYDOWN, VK_SPACE, 0x00390001UL);
            pWndEdit->PostMessage(WM_KEYUP, VK_SPACE, 0xC0390001UL);
           
            ::MessageBeep(-1);
        }
       
        this->SetActiveWindow();
    }

It does work if the destination application is 16-bit too. Unfortunately, pWndEdit is NULL if the destination application is 32-bit. It is OK if your application runs under Windows 3.1 only.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

17 Experts available now in Live!

Get 1:1 Help Now