PostMessage not working?

Jan 8, 2012 at 4:57pm
Dear reader,

I am trying to send some keystrokes to an already running application.
Everything works fine if I use any other function then PostMessage.

When I use PostMessage, nothing happens!!!

But I have been told that I need PostMessage if I want to be able to
send keystrokes even when (for w/e reason) the window I am sending it to is
not focussed.

I know it is not 100% safe but, it works good enough for what I want.
I hope someone can help me out!

A piece of code where I am using PostMessage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// bring the window to the front
        HWND GameWindow = FindWindow(0, L"Naamloos - Kladblok");
        SetForegroundWindow(GameWindow);
 
        // execute the loop
        for( int i = 0; i < amount; i++ ){
        // not the last loop so add a pause at the end
        if( i < (amount-1))
        {
            PostMessage(GameWindow, WM_KEYDOWN, 'G', 0);
            PostMessage(GameWindow, WM_KEYUP, 'G', 0);
            Sleep(2000);
        }
        // last loop so dont add a pause at the end
        else
        {
            PostMessage(GameWindow, WM_KEYDOWN, 'G', 0);
            PostMessage(GameWindow, WM_KEYUP, 'G', 0);
        }
        }


I am on Windows 7 with Qt Creator.
Last edited on Jan 8, 2012 at 4:59pm
Jan 8, 2012 at 5:50pm
closed account (DSLq5Di1)
According to google, "Naamloos - Kladblok" translates to "Untitled - Notepad", if you are sending key strokes to Notepad, you will need a handle to the edit control. You may also need a slight delay between the keydown and keyup messages.

By the looks of it though, you wish to send key strokes to a game window? it's not uncommon for PostMessage to be blocked via anti cheat software.

I'm getting sick of all these game hacking threads.
Jan 8, 2012 at 5:56pm
Dear sloppy9,

it has nothing to do with game hacking, the whole point is that I am learning C++ and this part I found interesting. I know it is possible since people have tools that do what I am wanting to do here, but I have no clue how they did it.

This is just a learning process for me, no more, no less.

And if you are sick of posts, then why bother reading this forum? might be best for you to move on and go somewhere else or dont visit forums at all anymore.

Besides that, the code works. If I change PostMessage for any other functions (sendkeys for example) then it works perfectly. It is just not working with PostMessage ;)
Jan 8, 2012 at 6:22pm
closed account (DSLq5Di1)
Apologies if I've misunderstood your intentions.

Besides that, the code works. If I change PostMessage for any other functions (sendkeys for example) then it works perfectly. It is just not working with PostMessage ;)
PostMessage does not function the same as SendKeys (.net ?) or "any" other function, see the first paragraph of my post above for a possible solution.
Jan 8, 2012 at 6:38pm
Awesome thank you a lot sloppy9.

I have tried various functions now, the only one which seems to work is keybd_event(VkKeyScan('H'),0,0,0);
Any other function like sendkey, PostMessage or anything else is simple not doing anything at all. No matter if I send it to Notepad, Paint or any other program.

The header <windows.h> is the right one though?

Anyways adding a pause didnt help, changing the name from Dutch to English didnt help either, the program will now freeze (ofcourse, since I have no check for window found or not).

I have the following headers file:

#include <QApplication>
#include <QWidget>
#include <QObject>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QPushButton>
#include <QMessageBox>
#include <QLabel>
#include <QSound>

#include <windows.h>

#include "PROCESS.h"

Side note: I am not english, so sorry for my bad english and if I do not fully understand something you wrote / meant ;)
Jan 9, 2012 at 2:06am
closed account (DSLq5Di1)
Been awhile since I've messed around with this stuff.. but give the following a try,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <windows.h>
#include <iostream>

struct extraKeyInfo
{
    unsigned short repeatCount;
    unsigned char scanCode;
    bool extendedKey, prevKeyState, transitionState;

    operator unsigned int()
    {
        return repeatCount | (scanCode << 16) | (extendedKey << 24) |
               (prevKeyState << 30) | (transitionState << 31);
    }
};

int main()
{
    HWND hNotepad = FindWindow(TEXT("Notepad"), NULL);

    if (!hNotepad)
        std::cout << "Notepad was not found!\n";
    else
    {
        HWND hEdit = FindWindowEx(hNotepad, NULL, TEXT("Edit"), NULL);

        if (!hEdit)
            std::cout << "Notepads edit control was not found!\n";
        else
        {
            SetForegroundWindow(hNotepad);

            for (char ch = 'a'; ch <= 'z'; ch++) // Output a-z in Notepad
            {
                short vkCode = LOBYTE(VkKeyScan(ch));
				
                extraKeyInfo lParam = {};
                lParam.scanCode = MapVirtualKey(vkCode, MAPVK_VK_TO_VSC);

                PostMessage(hEdit, WM_KEYDOWN, vkCode, lParam);

                lParam.repeatCount = 1;
                lParam.prevKeyState = true;
                lParam.transitionState = true;

                PostMessage(hEdit, WM_KEYUP, vkCode, lParam);
                Sleep(100);
            }
        }
    }
    std::cin.get();
    return 0;
}

See MSDN for the LPARAM details of WM_KEYDOWN/WM_KEYUP:-
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646281
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280

Also, play around with Spy++:-
http://mdb-blog.blogspot.com/2010/11/microsoft-spy-or-spyxx-for-download.html
Jan 9, 2012 at 11:11am
Awesome, a lot of stuff that I dont know yet but thats the whole point lol, learn learn and learn more :)
Thanks a lot for your answer sloppy, I will give this a try after I get home from work!
Jan 9, 2012 at 3:46pm
No clue what lparam does (my english is to bad to fully understand it) but I just filled in a number (29 lol) and its working now, thanks a lot sloppy, now this is working it means tearing it to pieces and start learing this lol. Thanks again you are awesome! :D
Topic archived. No new replies allowed.