Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Macro.h
- #pragma once
- #include <Windows.h>
- #include <iostream>
- #include <vector>
- class Macro
- {
- std::vector<CHAR> keys;
- std::vector<DWORD> pause;
- bool loop = false;
- UINT stopKey = VK_NUMPAD5;
- public:
- Macro(std::vector<CHAR> keys, std::vector<DWORD> pause, bool loop = false, UINT stopKey = VK_NUMPAD5);
- static void pressKey(CHAR keyParam);
- void run();
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Macro.cpp
- #include "Macro.h"
- Macro::Macro(std::vector<CHAR> keys, std::vector<DWORD> pause, bool loop, UINT stopKey){
- this->keys = keys;
- this->pause = pause;
- this -> loop = loop;
- this->stopKey = stopKey;
- }
- void Macro::pressKey(CHAR keyParam){
- SHORT key;
- UINT mappedkey;
- INPUT input = { 0 };
- key = VkKeyScan(keyParam);
- mappedkey = MapVirtualKey(LOBYTE(key), 0);
- input.type = INPUT_KEYBOARD;
- input.ki.dwFlags = KEYEVENTF_SCANCODE;
- input.ki.wScan = mappedkey;
- SendInput(1, &input, sizeof(input));
- Sleep(10);
- input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
- SendInput(1, &input, sizeof(input));
- }
- void Macro::run(){
- if (keys.size() != pause.size()) {
- std::cout << "Amount of keys and breaks must be equal!" << std::endl;
- return;
- }
- do {
- for (int i = 0; i < keys.size(); ++i) {
- pressKey(keys.at(i));
- Sleep(pause.at(i) * 1000 + 50); //always at least 50ms break time
- }
- if (GetAsyncKeyState(stopKey))//stop the loop if defined break key gets pressed.
- return;
- } while (loop);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Main.cpp
- #include <iostream>
- #include <Windows.h>
- #include <vector>
- #include "Macro.h"
- void test() {
- while (!GetAsyncKeyState(VK_NUMPAD0)) {
- Sleep(50);
- if (GetAsyncKeyState(VK_NUMPAD1)) { //my old version
- INPUT input = { 0 };
- input.type = INPUT_KEYBOARD;
- input.ki.wVk = VkKeyScan('w');
- SendInput(1, &input, sizeof(input));
- }
- if (GetAsyncKeyState(VK_NUMPAD2)) { //new version (King Gore)
- SHORT key;
- UINT mappedkey;
- INPUT input = { 0 };
- key = VkKeyScan('w');
- mappedkey = MapVirtualKey(LOBYTE(key), 0);
- input.type = INPUT_KEYBOARD;
- input.ki.dwFlags = KEYEVENTF_SCANCODE;
- input.ki.wScan = mappedkey;
- SendInput(1, &input, sizeof(input));
- Sleep(10);
- input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
- SendInput(1, &input, sizeof(input));
- }
- }
- }
- int main(){
- while (!GetAsyncKeyState(VK_NUMPAD0)) {
- Sleep(50);
- if (GetAsyncKeyState(VK_NUMPAD1)) {
- std::vector<CHAR> keys = { '1','2','3', '^', '5', '^' };
- std::vector<DWORD> pause{ 1,2,1,1,2,10 };
- Macro macro = Macro(keys, pause, true, VK_NUMPAD9);
- macro.run();
- }
- if (GetAsyncKeyState(VK_NUMPAD2)) {
- test();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement