How to make virtual input hardware input?

248 Views Asked by At

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?

0

There are 0 best solutions below