[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to implement SendKeys in C/C++

Posted on 1997-04-04
3
Medium Priority
?
3,937 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 900 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

834 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