Simulating KeyDown for creating Video Game Auto Run

Hi all,

I am new to C++ and attempting write a console application that make my character Auto Run in game.

I have written the part of reading the game through memory and been able to toggle Auto Run on and off.

But when I toggle the Auto Run I want to have the keys "Left Shift" & "W" pressed down.

I tried to use SendInput but when I enabled the Auto Run it crashed the game..

Here is my code so far for toggling Auto Run:
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
HWND hWnd = FindWindowA(NULL, "DayZ");

	if (hWnd == NULL)
	{
		SetConsoleTextAttribute(hConsole, 12);
		std::cout << "ERROR: DayZ Window Not Found" << std::endl;
		Sleep(3000);
		exit(-1);
	}
	else
	{
		DWORD ProcID;
		GetWindowThreadProcessId(hWnd, &ProcID);
		HANDLE DayZHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcID);

		if (ProcID == NULL)
		{
			SetConsoleTextAttribute(hConsole, 12);
			std::cout << "ERROR: Cannot obtain process." << std::endl;
			Sleep(3000);
			exit(-1);
		}
		else
		{
			for (;;)
			{
				if (EnableAutoRun)
				{
					keybd_event(VK_LSHIFT,0,0,0);
				}

				if (GetAsyncKeyState(VK_NUMPAD8) && !EnableAutoRun)
				{
					EnableAutoRun = true;
					SetConsoleTextAttribute(hConsole, 15);
					std::cout << "Auto Run: ";
					SetConsoleTextAttribute(hConsole, 2);
					std::cout << "Enabled" << std::endl;
					Sleep(200);
				}
				else if (GetAsyncKeyState(VK_NUMPAD8) && EnableAutoRun)
				{
					EnableAutoRun = false;
					SetConsoleTextAttribute(hConsole, 15);
					std::cout << "Auto Run: ";
					SetConsoleTextAttribute(hConsole, 4);
					std::cout << "Disabled" << std::endl;
					Sleep(200);
				}
			}
		}
	}



All help would be greatly appreciated!
I don't know if SendInput() would work for this. Games are different beasts from normal windowed applications. Usually (on Windows) they read the input state directly using DirectInput.

That aside, I'm pretty sure there's macro programs out there that would let you do this.
helios,

Okay, I know it is probably a stupid question I just wanted to try make my own so I can send to Friends without having to show them how to setup anything else.

And also to have the right to say "Yeah I made that" hahaha
Topic archived. No new replies allowed.