Key press event in game

Hello everyone! :)

I'm uber-beginner. I'm looking forward to making a simple app which would run certain Keyboard's buttons' presses (or mouse movement/press) at certain times in a single player game.

Output effect would be something like this:
-> Press A
-> wait 55ms
-> Release A
-> wait 2000ms
-> Press B
-> ... etc

I think the easiest solution to my problem would be to use some simple Macro scripts, or even if I wanted to use a programming language then AutoIt or Python or anything else could be a simplier choice. Problem is, I really need to use C++ for that.

Here's my code:

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
#include <windows.h>
#include <iostream>

int main()
{
    Sleep(2000);

    HWND handle = FindWindow(NULL, "GAME's window name");
    std::cout << "GAME's window address: " << handle;

    bool will_it_work = IsWindowEnabled(handle); // returns non-zero
    // number if Window can receive keyboard and mouse signals
    std::cout << "\nCan window receive keyboard/mouse signals: ";
    if (will_it_work==false)
        std::cout << "No.";
    else
        std::cout << "Yes.";


    keybd_event(VkKeyScan('z'), 0, 0, 0); // press z
    Sleep(200);
    keybd_event(VkKeyScan('z'), 0, KEYEVENTF_KEYUP, 0); // release z

    SetCursorPos(1200, 375); // moves cursor to (x, y)
}


but it seems SetCursorPos() and keybd_event does not take effect in the game. In fact I checked bunch of different games (single player!) and it doesn't take effect in any of them...

But it works in desktop, notepad, Google Chrome, etc. Just doesn't work in games... for some reason.

Can anyone recommend me other way to achieve my goal with C++?

Thanks for your time.
Last edited on
The games may not be listening to the windows events.
I can't recall.. maybe directinput has a 'send' command to fake a hardware event?
I am really fuzzy on how this stuff works anymore, but IIRC a program does not have to listen to windows events... it can read the hardware other ways.
Last edited on
Update: also tried with SendInput(); method --- doesn't work in various games.

Any help is appreciated.

Happy new year, by the way!
Topic archived. No new replies allowed.