Solved

How to implement SendKeys in C/C++

Posted on 1997-04-04
3
3,855 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

13 Experts available now in Live!

Get 1:1 Help Now