0

I wanted to make a strafe bot for garry's mod, and I came up with this:

#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "stdafx.h"

class KeyBot
{
private:
    INPUT _buffer[1];

public:
    KeyBot();
    void KeyUp(char key);
    void KeyDown(char key);
    void KeyClick(char key);
};

KeyBot::KeyBot()
{
    _buffer->type = INPUT_KEYBOARD;
    _buffer->ki.wScan = 0;
    _buffer->ki.time = 0;
    _buffer->ki.dwExtraInfo = 0;
}

void KeyBot::KeyUp(char key)
{
    _buffer->ki.wVk = key;
    _buffer->ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, _buffer, sizeof(INPUT));
}

void KeyBot::KeyDown(char key)
{
    _buffer->ki.wVk = key;
    _buffer->ki.dwFlags = 0;
    SendInput(1, _buffer, sizeof(INPUT));
}

void KeyBot::KeyClick(char key)
{
    KeyDown(key);
    Sleep(10);
    KeyUp(key);
}

char check_mouse(POINT xOne, POINT xTwo)
{
    GetCursorPos(&xOne);
    Sleep(1);
    GetCursorPos(&xTwo);
    if ((xTwo.x - xOne.x) > 0) {
        return 'D';
    }
    else {
        if ((xTwo.x - xOne.x) < 0) {
            return 'A';
        }
        else {
            if ((xTwo.x - xOne.x) == 0) {
                return 'N';
            }
        }
    }
}

int main()
{
    KeyBot bot;
    POINT xOne;
    POINT xTwo;

    while (1) {
        if (check_mouse(xOne, xTwo) == 'A') {
            bot.KeyUp(0x44);
            bot.KeyDown(0x41);
        }
        if (check_mouse(xOne, xTwo) == 'D') {
            bot.KeyUp(0x41);
            bot.KeyDown(0x44);
        }
        if (check_mouse(xOne, xTwo) == 'N') {
            bot.KeyUp(0x44);
            bot.KeyUp(0x41);
        }
    }
    return 0;
}

This works fine, when opening notepad or game chat, it holds a when I move my mouse left, and it holds d when I move my mouse to the right. The problem is, that, when I am in game, it doesn't move my character at all. I think the problem is that this is a virtual key press and not a hardware one, and I don't know how to change this. can someone modify my code in order for this to work?

  • It really depends on how the game gets input. A fair few games use some low-level mechanism (e.g. a "hardware one" to use your words) to grab input, to improve responsiveness or some other characteristic. If a game installs a dedicated kernel mode driver, for example, the means of interacting with that driver will be whatever the game developer chose it to be - that is just one tactic to make it harder for people to cheat in the game. Without knowing how your particular game grabs keystrokes, nobody can help you. – Peter Sep 19 '18 at 14:00
  • 1
    Please edit post to define `INPUT`. – Thomas Matthews Sep 19 '18 at 14:21
  • Why do you have an array of 1 element? – Thomas Matthews Sep 19 '18 at 14:22
  • You should call `check_mouse()` once before the `if` statements. There is a possibility that each call could return a different value. – Thomas Matthews Sep 19 '18 at 14:23
  • You may want to prefer using a `switch` statement in the `while` instead of `if`. Or replace the `if`s with an `std::map`, since you are associating a letter with key up and key down values. – Thomas Matthews Sep 19 '18 at 14:24
  • @ThomasMatthews INPUT should be [this one](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-taginput) – Karsten Koop Sep 19 '18 at 15:02
  • See also [this question](https://stackoverflow.com/questions/18647053/sendinput-not-equal-to-pressing-key-manually-on-keyboard-in-c) – Karsten Koop Sep 19 '18 at 15:09

0 Answers0