im using keybd_event(); and i want use SendMessage(); to send keystroke to notepad, can this be done?
2 Answers
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.

- 40,644
- 36
- 176
- 219
-
Does, in the SendInput() exmaple, the KEYEVENTF_UNICODE care about the type of text, which may be a char too? I see no KEYEVENTF_SCANCODE but you put your character in wScan. Is GetMessageExtraInfo() necessary? – ManuelSchneid3r Oct 17 '12 at 22:19
-
1@DevNoob: No, this should work for both Unicode and non-Unicode builds. (I just tested it.) Yes, GetMessageExtraInfo() _appears_ to be necassary because the documentation specifies that it's required. By the way, the answers to your questions can also be found by reading MSDN documentation for the corresponding function and types using the link in my answer. – Sam Oct 18 '12 at 09:13
-
@DevNoob, I'm not completely sure what you mean about `KEYEVENTF_KEYUP` being left out. I'm also not sure what you mean about the part of the documentation you quoted. If you're wondering why both key-down and key-up keyboard events are required, it's because these are being used to generated corresponding WM_KEYDOWN and `WM_KEYUP` window messages for the destination window. We're working on a low level, and I don't think the Windows API provides a `WM_KEYPRESS` message to represent a single key _press_. – Sam Oct 18 '12 at 23:33
-
@DevNoob, using `WM_KEYDOWN` and `WM_KEYUP` is actually a pretty good solution because it's *very* compatible. This is because these Window messages are standard for almost all Windows applications. Separating them gives us more control, too. The only types of Windows applications I can think of that wouldn't support this approach are those that use DirectInput instead. A disadvantage of this is SendInput only sends the messages to the currently-focused window, which could be something like a menu rather than the text field. – Sam Oct 18 '12 at 23:36
-
Sorry copy-paste mistake. I did, but MS should cleary distinguish between the character that is sent and the one that has to be used as input. Thats what i missunderstood at the first glace. What I still dont understand is why KEYEVENTF_SCANCODE is left out. As per documentation "... If specified, wScan identifies the key and wVk is ignored." From [here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271(v=vs.85).aspx) – ManuelSchneid3r Oct 19 '12 at 00:15
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.

- 50,847
- 7
- 72
- 76
-
-
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. Jan 22 '10 at 00:12
-
You can't use SendMessage() to send keystrokes. You can't control the keyboard state. Particularly the Shift, Control and Alt keys. – Hans Passant Jan 22 '10 at 00:23
-
2Also 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 Jan 22 '10 at 00:33
-
1you don't need to send keystrokes to input text. the OP already knows about `keybd_event`. the question is about using `SendMessage` – jspcal Jan 22 '10 at 00:50
-
The question is confusing, since it says use "SendMessage(); to send keystroke". Yes, if keystrokes are to be sent then SendMessage does not work but if SendMessage is to be used then the question is not about sending keystrokes. – Sam Hobbs Apr 16 '16 at 02:12