This site hosted by Free.ProHosting.com
Google

How can I send keys to another application?

Applies To: C++Builder 1 or higher
Category: Knowledge Base

I have to send keys to an opened window such as Notepad. How can I do this in C++Builder?
 
 
 1.
  Start a new project using File | New Application.
 
 2.
  Add a Button and edit the OnClick event:
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    // Get the handle of the window
    HANDLE notepad = FindWindow("Notepad", 0); 
    if(notepad != NULL) 
    { 
        // Activate Notepad 
        BringWindowToTop(notepad); 
        // Send key 
        keybd_event('A', 0, 0, 0); 
        keybd_event('A', 0, KEYEVENTF_KEYUP, 0); 
    } 
    else 
    { 
        ShowMessage("Notepad not opened!"); 
    } 
}
 
 3.
  Make sure that Notepad is opened.
 
The Win32 API function keybd_event sends keystrokes to another application.

C++Builder Developer's Network
Copyright © Yoto Yotov