tags:

views:

167

answers:

2

im using keybd_event(); and i want use SendMessage(); to send keystroke to notepad, can this be done?

+1  A: 

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.

jspcal
How does that send it to a window ?
H4cKL0rD
It doesn't. You need to pass a window handle in the `hWnd` parameter. Also, the window handle is the *first* parameter, not the third.
Anon.
You can't use SendMessage() to send keystrokes. You can't control the keyboard state. Particularly the Shift, Control and Alt keys.
Hans Passant
Also WM_SETTEXT doesn't send keystrokes. In any case you can't do it reliably with SendMessage() as nobugz mentioned. See http://blogs.msdn.com/oldnewthing/archive/2005/05/30/423202.aspx
shf301
you don't need to send keystrokes to input text. the OP already knows about `keybd_event`. the question is about using `SendMessage`
jspcal
A: 

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 * 2 ];
    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.

Sam