How to send keystrokes to a window?

using SendMessage to insert text into the edit buffer (which it sounds like you want):

HWND notepad = FindWindow(_T("Notepad"), NULL);
HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL);
SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello"));

if you need keycodes and arbitrary keystrokes, you can use SendInput() (available in 2k/xp and preferred), or keybd_event()` (which will end up calling SendInput in newer OSs) some examples here:

http://www.codeguru.com/forum/showthread.php?t=377393

there's also WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR events for SendMessage which you might be interested in.


keybd_event() has been superseded with SendInput(), so it's best to use that. Here is some example code to do what you have asked. You can either directly edit the Notepad window's edit control using SendMessage(), or you can use SendInput() to synthesise keystrokes to be sent to the window.

Using SendInput():

int SendKeystrokesToNotepad( const TCHAR *const text )
{
    INPUT *keystroke;
    UINT i, character_count, keystrokes_to_send, keystrokes_sent;
    HWND notepad;

    assert( text != NULL );

    //Get the handle of the Notepad window.
    notepad = FindWindow( _T( "Notepad" ), NULL );
    if( notepad == NULL )
        return 0;

    //Bring the Notepad window to the front.
    if( !SetForegroundWindow( notepad ) )
        return 0;

    //Fill in the array of keystrokes to send.
    character_count = _tcslen( text );
    keystrokes_to_send = character_count * 2;
    keystroke = new INPUT[ keystrokes_to_send ];
    for( i = 0; i < character_count; ++i )
    {
        keystroke[ i * 2 ].type = INPUT_KEYBOARD;
        keystroke[ i * 2 ].ki.wVk = 0;
        keystroke[ i * 2 ].ki.wScan = text[ i ];
        keystroke[ i * 2 ].ki.dwFlags = KEYEVENTF_UNICODE;
        keystroke[ i * 2 ].ki.time = 0;
        keystroke[ i * 2 ].ki.dwExtraInfo = GetMessageExtraInfo();

        keystroke[ i * 2 + 1 ].type = INPUT_KEYBOARD;
        keystroke[ i * 2 + 1 ].ki.wVk = 0;
        keystroke[ i * 2 + 1 ].ki.wScan = text[ i ];
        keystroke[ i * 2 + 1 ].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
        keystroke[ i * 2 + 1 ].ki.time = 0;
        keystroke[ i * 2 + 1 ].ki.dwExtraInfo = GetMessageExtraInfo();
    }

    //Send the keystrokes.
    keystrokes_sent = SendInput( ( UINT )keystrokes_to_send, keystroke, sizeof( *keystroke ) );
    delete [] keystroke;

    return keystrokes_sent == keystrokes_to_send;
}

Using SendMessage():

int SendKeystrokesToNotepad( const TCHAR *const text )
{
    HWND notepad, edit;

    assert( text != NULL );

    //Get the handle of the Notepad window.
    notepad = FindWindow( _T( "Notepad" ), NULL );
    if( notepad == NULL )
        return 0;

    //Get the handle of the Notepad window's edit control.
    edit = FindWindowEx( notepad, NULL, _T( "Edit" ), NULL );
    if( edit == NULL )
        return 0;

    SendMessage( edit, EM_REPLACESEL, ( WPARAM )TRUE, ( LPARAM )text );
    return 1;
}

I hope that helps.