On Windows I have used a very primitive technique successfully with several applications in the past. I've just tested it and it still works on my Windows 7. First you have to find the handle of the window that has to receive the keystrokes. This is often not the main window but one of the child windows (for example and editbox or a button...). Then you post/send messages to this child window (preferably post). You can simulate keys/mouse by sending WM_KEYDOWN, WM_KEYUP, WM_CHAR, WM_LBUTTONDOWN, ... messages to the window. This technique often has several advantages over the SendKey() api, for example you don't have to put the focus on your target application but it isn't guaranteed to work. Most applications will probably work with it though.
Here is a piece of C/C++ code that does exactly what you wanted with notepad (at least on my windows 7):
int main()
{
HWND main_hwnd = FindWindowW(L"Notepad", NULL);
if (!main_hwnd)
return 1;
HWND edit_hwnd = GetWindow(main_hwnd, GW_CHILD);
if (!edit_hwnd)
return 1;
do
{
wchar_t wnd_class[0x100];
if (GetClassNameW(edit_hwnd, wnd_class, sizeof(wnd_class)/sizeof(wnd_class[0])))
{
if (0 == wcsicmp(L"Edit", wnd_class))
break;
}
edit_hwnd = GetWindow(edit_hwnd, GW_HWNDNEXT);
} while (edit_hwnd);
if (!edit_hwnd)
return 1;
for (;;)
{
PostMessageW(edit_hwnd, WM_CHAR, 'v', 0);
Sleep(3000);
if (!IsWindow(edit_hwnd))
break;
PostMessageW(edit_hwnd, WM_CHAR, ' ', 0);
Sleep(3000);
if (!IsWindow(edit_hwnd))
break;
}
return 0;
}
Note: I used the Spy++ utility to find out the window class of the main window ("Notepad") and to find out that the edit control is its direct child hwnd.