Имитация нажатия клавиш с помощью PostMessage работает только в некоторых приложениях?

Мой подход к этой проблеме оказался верным только в нескольких программах. Почему он не универсален?

Отлично работает на:

Firefox
Visual Studio Text Editor

Unfortunately, in some cases nothing happens(even if I click into a textbox area before execution of my program):

Google Chrome Notepad

GetLastError всегда возвращает 0, даже используя SendMessage вместо PostMessage. Не могли бы вы указать на мою ошибку?

#include <Windows.h>
#include <iostream>

int main()
{
    HWND hCurrentWindow;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++) //simulate 500 keystrokes of 'E'.
        {
            PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,NULL);
            PostMessage(hCurrentWindow,WM_KEYUP,0x45,NULL);
        }

    std::cout<<GetLastError()<<std::endl;

    system("Pause");
    return 0;
}

UPDATE after Maximus sugestion

#include <Windows.h>
#include <iostream>

int main()
{
    HWND hCurrentWindow;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    if(!hCurrentWindow)
        std::cout<<"Failed get set the window handle\n";

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++)
        {
            PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,0x45);
            PostMessage(hCurrentWindow,WM_KEYUP,0x45,0x45);
        }

    std::cout<<GetLastError()<<std::endl;

    system("Pause");
    return 0;
}

Там нет никакой разницы в эффекте.

UPDATE after Rob Kennedy's comment and Hans Passant's answer

#include <Windows.h>
#include <iostream>

int main()
{
    HWND hCurrentWindow;
    DWORD procID;
    GUITHREADINFO currentWindowGuiThreadInfo;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    if(!hCurrentWindow)
        std::cout<<"Failed get main the window handle\n";

    GetWindowThreadProcessId(hCurrentWindow,&procID); 
    GetGUIThreadInfo(procID,&currentWindowGuiThreadInfo);               
    hCurrentWindow = currentWindowGuiThreadInfo.hwndFocus;

    if(!hCurrentWindow)
        std::cout<<"Failed get the child window handle\n";

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++)
        {
            PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
            PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
        }

    std::cout<<GetLastError()<<std::endl;

    system("Pause");
    return 0;
}

Теперь "прозрачный" сообщения отправляются каждый раз. GetLastError () говорит:

ERROR_INVALID_WINDOW_HANDLE

1400 (0x578)

Invalid window handle.

GetLastError() "fixed"

int main()
{
    HWND hCurrentWindow;
    DWORD procID;
    GUITHREADINFO currentWindowGuiThreadInfo;

    Sleep(5000);

    hCurrentWindow = GetForegroundWindow();

    if(!hCurrentWindow)
        std::cout<<"Failed get main the window handle\n";

    GetWindowThreadProcessId(hCurrentWindow,&procID); 
    GetGUIThreadInfo(procID,&currentWindowGuiThreadInfo);               
    hCurrentWindow = currentWindowGuiThreadInfo.hwndFocus;

    if(!hCurrentWindow)
        std::cout<<"Failed get the child window handle\n";

    std::cout<<"GO!!!\n";

    for(int i=0; i<500; i++)
        {

            if(!PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC))) std::cout<<GetLastError()<<std::endl;
            if(!PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC)))   std::cout<<GetLastError()<<std::endl;
        }



    system("Pause");
    return 0;
}

... выходы1400 тысячи раз. Кроме этого ничего не изменилось.

Ответы на вопрос(4)

Ваш ответ на вопрос