Solved

How to implement SendKeys in C/C++

Posted on 1997-04-04
3
3,876 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
[X]
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
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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