Das Simulieren des Tastendrucks mit PostMessage funktioniert nur in einigen Anwendungen?

Mein Ansatz zu diesem Problem stellte sich nur in mehreren Programmen als richtig heraus. Warum ist es nicht universell?

Funktioniert gut auf:

Feuerfuchs
Visual Studio-Texteditor

Leider passiert in einigen Fällen nichts (auch wenn ich vor der Ausführung meines Programms in einen Textfeldbereich klicke):

Google ChromeNotizblock

GetLastError gibt immer 0 zurück, auch wenn SendMessage anstelle von PostMessage verwendet wird. Können Sie meinen Fehler aufzeigen?

#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 nach Maximus Vorschlag

#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;
}

Es gibt keinen Unterschied in der Wirkung.

UPDATE nach dem Kommentar von Rob Kennedy und der Antwort von Hans Passant

#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;
}

Jetzt werden jedes Mal "transparente" Nachrichten gesendet. GetLastError () sagt:

ERROR_INVALID_WINDOW_HANDLE

1400 (0x578)

Invalid window handle.

GetLastError () "behoben"

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;
}

... Ausgänge1400 tausendmal. Ansonsten hat sich nichts geändert.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage