Specs
- OS: Windows 10
- Programming Language: C++14
- Compiler: MSVC 2019
- IDE: CLion 2019.3.3
Code:
#define WINVER 0x0500
#include <windows.h>
#include <string>
void press_enter() {
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press enter
ip.ki.wVk = 0x0D;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
Sleep(25);
}
void press_keys(std::string& text_to_write) {
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Load current window's keyboardLayout
HKL kbl = GetKeyboardLayout(0);
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
for (char& c : text_to_write) {
// Press the corresponding 'c' key
ip.ki.wVk = VkKeyScanEx(c, kbl);; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
Sleep(25);
}
}
void give_100000(std::string& item) {
for (int i = 0; i < 10; i++) {
press_keys(item);
Sleep(25);
press_enter();
Sleep(25);
press_enter();
Sleep(25);
}
}
int main() {
// Pause for 5 seconds.
Sleep(5000);
std::string lumber = "lumberjack";
std::string food = "cheese steak jimmy's";
std::string gold = "robin hood";
std::string stone = "rock on";
give_100000(lumber);
give_100000(food);
give_100000(gold);
give_100000(stone);
// Exit normally
return 0;
}
What this program does
I'm still very much a beginner in C++. I wrote this program as a little challenge and to practice my C++. It simulates keyboard presses, specifically to type cheats quickly, so that I get a bunch of resources in Age of Empires II.
The problem
This code works perfectly as is. It does what I want it to do. The thing is, there is repeated code inside both press_enter()
and press_keys()
functions, namely:
INPUT ip;
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
So I wanted to fix this.
What I tried
I thought I could just bring that piece of code outside all functions (right below the #includes) and make them act as global variables, so that ip
is accessible by all functions. But doing this made CLion complain, and compiling gave me a gigantic list of incomprehensible errors (I can post if needed). When I hover over any of the 4 lines that start with ip.
, CLion says: " Unknown type name 'ip' ". I don't understand this, since ip
was declared literally 2 lines above.
What I'm looking for
As I said, I am still a beginner in C++, so I would really like to understand what this means, if I'm missing some fundamental concept, and a way to make it work without unnecessarily repeating code.