Advertisement
Guest User

Source Code

a guest
Jan 2nd, 2019
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include <windows.h>
  2. #include <wtypes.h>
  3. #include <sstream>
  4. #include <vector>
  5. #include <iostream>
  6. #include <thread>
  7. #include <string>
  8. #include <map>
  9.  
  10. bool getKeyState_(unsigned VKID) {
  11.     return ((GetAsyncKeyState(VKID) & 0x8000) != 0);
  12. }
  13.  
  14. class MouseLocker {
  15. public:
  16.     short m_resolutionHeight;
  17.     short m_resolutionWidth;
  18.     short m_update_interval;
  19.  
  20.     unsigned __int8 m_virtualkey_code;
  21.    
  22.     bool m_thread_terminator;
  23.     bool m_cursor_worker_active;
  24.     bool m_debug_thread_active;
  25.    
  26.     bool m_makesound;
  27.  
  28.     short m_soundFrequency;
  29.     short m_soundDuration;
  30.  
  31.     std::thread* m_cursor_thread;
  32.     std::thread* m_debug_thread;
  33.  
  34.     std::pair<std::string, long> executeCommand_(std::string command) {
  35.         std::string command_output;
  36.  
  37.         FILE* read_pipe = _popen(command.c_str(), "r");
  38.  
  39.         if (!read_pipe) {
  40.             std::cout << "Failed to open pipe to system command: " << command << std::endl;
  41.             return std::make_pair(command_output, 1);
  42.         }
  43.  
  44.         char buffer[1024];
  45.  
  46.         try {
  47.             while (fgets(buffer, sizeof(buffer), read_pipe) != NULL)
  48.                 command_output.append(buffer);
  49.         } catch (...) {
  50.             std::cout << "Exception when reading from pipe." << std::endl;
  51.             _pclose(read_pipe);
  52.             return std::make_pair(command_output, 2);
  53.         }
  54.        
  55.         return std::make_pair(command_output, 0);
  56.     }
  57.  
  58.     void cursor_worker_() {
  59.         std::pair<short, short> coordinates;
  60.         coordinates.first = std::round(m_resolutionWidth / 2);
  61.         coordinates.second = std::round(m_resolutionHeight / 2);
  62.  
  63.         while (!m_thread_terminator) {
  64.             while (m_cursor_worker_active) {
  65.                 SetCursorPos(coordinates.first, coordinates.second);
  66.                 Sleep(m_update_interval);
  67.             }
  68.             Sleep(500);
  69.         }
  70.     }
  71.  
  72.     void debug_info_worker_() {
  73.         while (!m_thread_terminator && m_debug_thread_active) {
  74.             system("cls");
  75.             std::stringstream vkid_hex; vkid_hex << std::uppercase << std::hex << (int)m_virtualkey_code;
  76.             std::cout << "Virtual Key Code: 0x" << vkid_hex.str() << std::endl;
  77.             std::cout << "Virtual Key State: " << (int)((GetAsyncKeyState(m_virtualkey_code) & 0x8000) != 0) << " (SLOW) " << std::endl;
  78.             std::cout << "Cursor Worker Active: " << m_cursor_worker_active << std::endl;
  79.             std::cout << "Base Resolution: " << m_resolutionWidth << "x" << m_resolutionHeight << std::endl;
  80.             std::cout << "Cursor Target Position: " << std::round(m_resolutionWidth / 2) << "x" << std::round(m_resolutionHeight / 2) << std::endl;
  81.             Sleep(500);
  82.         }
  83.     }
  84.  
  85.     MouseLocker() {
  86.         /* Resolution setup */
  87.         RECT desktopGeometry;
  88.         HWND desktopWindow = GetDesktopWindow();
  89.         GetWindowRect(desktopWindow, &desktopGeometry);
  90.         m_resolutionHeight = desktopGeometry.bottom;
  91.         m_resolutionWidth = desktopGeometry.right;
  92.  
  93.         /* Cursor Thread Setup */
  94.         m_virtualkey_code = 0x6A;
  95.         m_update_interval = 30;
  96.  
  97.         /* Threading Terminator Setup */
  98.         m_cursor_worker_active = false;
  99.         m_thread_terminator = false;
  100.         m_debug_thread_active = false;
  101.  
  102.         /* Sound Setup */
  103.         m_makesound = true;
  104.         m_soundFrequency = 1000;
  105.         m_soundDuration = 10;
  106.     }
  107.  
  108.     void spawn () {
  109.         /* Thrading Setup */
  110.         m_cursor_thread = new std::thread(std::bind(&MouseLocker::cursor_worker_, this));
  111.         m_debug_thread = new std::thread(std::bind(&MouseLocker::debug_info_worker_, this));
  112.     }
  113.  
  114.     ~MouseLocker() {
  115.         m_cursor_worker_active = false;
  116.         m_thread_terminator = true;
  117.  
  118.         Sleep(1000);
  119.  
  120.         delete m_cursor_thread;
  121.         delete m_debug_thread;
  122.     }
  123.  
  124.     void start() {
  125.         while (true) {
  126.             bool VKID_State = (GetAsyncKeyState(m_virtualkey_code) & 0x8000) != 0;
  127.             if (VKID_State) {
  128.                 m_cursor_worker_active = !m_cursor_worker_active;
  129.                 if(m_makesound) Beep(m_soundFrequency, m_soundDuration);
  130.             }
  131.             Sleep(100);
  132.         }
  133.     }
  134. };
  135.  
  136. int main(int argc, char* argv[]) {
  137.     MouseLocker mouseLocker = MouseLocker();
  138.     std::vector<std::string> arguments;
  139.  
  140.     std::map<std::string, unsigned long*> resolver{
  141.         { "vkid", (unsigned long*)&mouseLocker.m_virtualkey_code },
  142.         { "width",(unsigned long*)&mouseLocker.m_resolutionWidth },
  143.         { "height", (unsigned long*)&mouseLocker.m_resolutionHeight },
  144.         { "debug", (unsigned long*)&mouseLocker.m_debug_thread_active },
  145.         { "interval", (unsigned long*)&mouseLocker.m_update_interval },
  146.         {"sound", (unsigned long*)&mouseLocker.m_makesound},
  147.         {"sound_freq", (unsigned long*)&mouseLocker.m_soundFrequency},
  148.         {"sound_dur", (unsigned long*)&mouseLocker.m_soundDuration}
  149.     };
  150.  
  151.     for (int i = 1; i < argc; i++) arguments.push_back(std::string(argv[i]));
  152.     for (int i = 0; i < arguments.size(); i++) {
  153.         std::string& argument = arguments.at(i);
  154.         std::vector<std::string> split;
  155.         std::string buffer;
  156.  
  157.         for (int c = 0; c < argument.size(); c++) {
  158.             char& cchar = argument.at(c);
  159.             if (cchar == '=') {
  160.                 split.push_back(buffer);
  161.                 buffer.clear();
  162.             } else {
  163.                 buffer.push_back(cchar);
  164.             }
  165.         }
  166.  
  167.         if (buffer.size() > 0) split.push_back(buffer);
  168.         buffer.clear();
  169.        
  170.         std::string key = split.at(0);
  171.         unsigned long value;
  172.  
  173.         bool hexadecimal = false;
  174.         for (int c = 0; c < split.at(1).size(); c++) {
  175.             char& cchar = split.at(1).at(c);
  176.             if (cchar < 48 || cchar > 57) {
  177.                 hexadecimal = true;
  178.                 break;
  179.             }
  180.         }
  181.  
  182.         if (hexadecimal) {
  183.             std::stringstream stream;
  184.             stream << std::hex << split.at(1);
  185.             stream >> value;
  186.         } else {
  187.             value = std::stoi(split.at(1));
  188.         }
  189.  
  190.         *resolver.at(key) = value;
  191.     }
  192.  
  193.     mouseLocker.spawn();
  194.     mouseLocker.start();
  195.  
  196.     std::cin.get();
  197.     return EXIT_SUCCESS;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement