Solved

How to implement SendKeys in C/C++

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

785 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