Advertisement
Guest User

Key presser for calculator emulators / simulators

a guest
Dec 10th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #define _WIN32_WINNT 0x0A00
  2. #define WIN_VER      0x0A00
  3.  
  4. #define MAPVK_VK_TO_VSC     (0)
  5.  
  6. #include <iostream>
  7. #include <windows.h>
  8. #include <fstream>
  9.  
  10. struct keymap_t {
  11.     int vkey;
  12.     POINT pt;
  13. };
  14.  
  15. keymap_t keymaps [256];
  16. int n_keymap;
  17.  
  18. int main(int argc, char** argv) {
  19.     char name [1024], title [1024];
  20.     std::cout << "External keyset for calculator emulators\n"
  21.                  "          Use Ctrl+C to exit\n";
  22.     std::cout << "Click at calculator emulator in 3 seconds\n";
  23.     RECT pos; HWND calc_hwnd;
  24.     Sleep(3000);
  25.     calc_hwnd = GetForegroundWindow();
  26.     GetWindowRect(calc_hwnd, &pos);
  27.     GetWindowTextA(calc_hwnd, title, 1024);
  28.     SetCursorPos(pos.left, pos.top);
  29.     std::cout << pos.left << "," << pos.top << ","
  30.     << pos.right << "," << pos.bottom << "\n" << title << "\n";
  31.  
  32.  
  33.     std::cout << "Load data / save data? (L / S) : ";
  34.     std::cin.getline(name, 1024);
  35.     if (strcmp(name, "L") == 0) {
  36.         std::cout << "File name: ";
  37.         std::cin.getline(name, 1024);
  38.         std::ifstream f (name);
  39.  
  40.         f >> n_keymap;
  41.         for (int i = 0; i < n_keymap; i++) {
  42.             f >> keymaps[i].pt.x >> keymaps[i].pt.y >> keymaps[i].vkey;
  43.         }
  44.         f.close();
  45.     } else if (strcmp(name, "S") == 0) {
  46.         std::cout << "File name: ";
  47.         std::cin.getline(name, 1024);
  48.         std::ofstream f (name);
  49.  
  50.         std::cout << "Enter number of key: ";
  51.         std::cin >> n_keymap;
  52.         f << n_keymap << "\n";
  53.         while ((GetAsyncKeyState(VK_RETURN) & 0x8000) != 0) {}
  54.         // ignore the last Enter (if any)
  55.         std::cout << "Move cursor to position of each key and then press key.\n"
  56.         "If mispressed key then click at this console window and press Delete button.\n";
  57.         int j;
  58.         for (int i = 0; i < n_keymap; i++) {
  59.             std::cout << "Press key " << i << ": ";
  60.             while (true) {
  61.                 Sleep(20);
  62.                 for(j = 0x08; j < 0xFF; j++) {
  63.                     if ((GetAsyncKeyState(j) & 0x8001) == 0x8001) {
  64.                         if (GetForegroundWindow() == calc_hwnd) { // explicit short circuit and &&
  65.                             keymaps[i].vkey = j;
  66.                             goto break_while1;
  67.                         } else if (j == VK_DELETE && i > 0) {
  68.                             i--;
  69.                             std::cout << "-DEL-\nPress key " << i << ": ";
  70.                         };
  71.                     }
  72.                 }
  73.             } break_while1:
  74.             GetKeyNameTextA(MapVirtualKeyA(j, MAPVK_VK_TO_VSC) << 16, name, 1024);
  75.             GetCursorPos(&keymaps[i].pt);
  76.  
  77.             keymaps[i].pt.x -= pos.left;
  78.             keymaps[i].pt.y -= pos.top;
  79.  
  80.             std::cout << j << " (" << name << ") at (" << keymaps[i].pt.x << ", " << keymaps[i].pt.y << ")\n";
  81.             f << keymaps[i].pt.x << " " << keymaps[i].pt.y << " " << keymaps[i].vkey << "\n";
  82.         }
  83.         f.close();
  84.     } else {
  85.         std::cout << "Unrecognized\n";
  86.         std::cin >> name;
  87.         return 0;
  88.     }
  89.     INPUT inp {0};
  90.     while (true) {
  91.         Sleep(20);
  92.         for(int i = 0; i < n_keymap; i++) {
  93.             if ((GetAsyncKeyState(keymaps[i].vkey) & 0x8001) == 0x8001) {
  94.                 if (GetForegroundWindow() == calc_hwnd) {
  95.                     GetWindowRect(calc_hwnd, &pos);
  96.                     SetCursorPos(keymaps[i].pt.x + pos.left, keymaps[i].pt.y + pos.top);
  97.                     inp.type = INPUT_MOUSE;
  98.                     inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
  99.                     SendInput(1, &inp, sizeof(inp));
  100.                     inp.type = INPUT_MOUSE;
  101.                     inp.mi.dwFlags = MOUSEEVENTF_LEFTUP;
  102.                     SendInput(1, &inp, sizeof(inp));
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement