Advertisement
CasualGamer

Untitled

May 11th, 2020 (edited)
4,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //Macro.h
  3.  
  4. #pragma once
  5.  
  6. #include <Windows.h>
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. class Macro
  11. {
  12.     std::vector<CHAR> keys;
  13.     std::vector<DWORD> pause;
  14.     bool loop = false;
  15.     UINT stopKey = VK_NUMPAD5;
  16.  
  17. public:
  18.     Macro(std::vector<CHAR> keys, std::vector<DWORD> pause, bool loop = false, UINT stopKey = VK_NUMPAD5);
  19.     static void pressKey(CHAR keyParam);
  20.     void run();
  21. };
  22.  
  23. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. //Macro.cpp
  25.  
  26. #include "Macro.h"
  27.  
  28. Macro::Macro(std::vector<CHAR> keys, std::vector<DWORD> pause, bool loop, UINT stopKey){
  29.     this->keys = keys;
  30.     this->pause = pause;
  31.     this -> loop = loop;
  32.     this->stopKey = stopKey;
  33. }
  34.  
  35. void Macro::pressKey(CHAR keyParam){
  36.     SHORT key;
  37.     UINT mappedkey;
  38.     INPUT input = { 0 };
  39.     key = VkKeyScan(keyParam);
  40.     mappedkey = MapVirtualKey(LOBYTE(key), 0);
  41.     input.type = INPUT_KEYBOARD;
  42.     input.ki.dwFlags = KEYEVENTF_SCANCODE;
  43.     input.ki.wScan = mappedkey;
  44.     SendInput(1, &input, sizeof(input));
  45.     Sleep(10);
  46.     input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
  47.     SendInput(1, &input, sizeof(input));
  48. }
  49.  
  50. void Macro::run(){
  51.     if (keys.size() != pause.size()) {
  52.         std::cout << "Amount of keys and breaks must be equal!" << std::endl;
  53.         return;
  54.     }
  55.     do {
  56.         for (int i = 0; i < keys.size(); ++i) {
  57.             pressKey(keys.at(i));
  58.             Sleep(pause.at(i) * 1000 + 50); //always at least 50ms break time
  59.         }
  60.         if (GetAsyncKeyState(stopKey))//stop the loop if defined break key gets pressed.
  61.             return;
  62.     } while (loop);
  63. }
  64.  
  65. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. //Main.cpp
  67. #include <iostream>
  68. #include <Windows.h>
  69. #include <vector>
  70.  
  71. #include "Macro.h"
  72.  
  73. void test() {
  74.     while (!GetAsyncKeyState(VK_NUMPAD0)) {
  75.         Sleep(50);
  76.         if (GetAsyncKeyState(VK_NUMPAD1)) { //my old version
  77.             INPUT input = { 0 };
  78.             input.type = INPUT_KEYBOARD;
  79.             input.ki.wVk = VkKeyScan('w');
  80.             SendInput(1, &input, sizeof(input));
  81.         }
  82.         if (GetAsyncKeyState(VK_NUMPAD2)) { //new version (King Gore)
  83.             SHORT key;
  84.             UINT mappedkey;
  85.             INPUT input = { 0 };
  86.             key = VkKeyScan('w');
  87.             mappedkey = MapVirtualKey(LOBYTE(key), 0);
  88.             input.type = INPUT_KEYBOARD;
  89.             input.ki.dwFlags = KEYEVENTF_SCANCODE;
  90.             input.ki.wScan = mappedkey;
  91.             SendInput(1, &input, sizeof(input));
  92.             Sleep(10);
  93.             input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
  94.             SendInput(1, &input, sizeof(input));
  95.         }
  96.     }
  97. }
  98.  
  99. int main(){
  100.     while (!GetAsyncKeyState(VK_NUMPAD0)) {
  101.         Sleep(50);
  102.         if (GetAsyncKeyState(VK_NUMPAD1)) {
  103.             std::vector<CHAR> keys = { '1','2','3', '^', '5', '^' };
  104.             std::vector<DWORD> pause{ 1,2,1,1,2,10 };
  105.             Macro macro = Macro(keys, pause, true, VK_NUMPAD9);
  106.             macro.run();
  107.         }
  108.         if (GetAsyncKeyState(VK_NUMPAD2)) {
  109.             test();
  110.         }
  111.     }
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement