Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WIN32_WINNT 0x0A00
- #define WIN_VER 0x0A00
- #define MAPVK_VK_TO_VSC (0)
- #include <iostream>
- #include <windows.h>
- #include <fstream>
- struct keymap_t {
- int vkey;
- POINT pt;
- };
- keymap_t keymaps [256];
- int n_keymap;
- int main(int argc, char** argv) {
- char name [1024], title [1024];
- std::cout << "External keyset for calculator emulators\n"
- " Use Ctrl+C to exit\n";
- std::cout << "Click at calculator emulator in 3 seconds\n";
- RECT pos; HWND calc_hwnd;
- Sleep(3000);
- calc_hwnd = GetForegroundWindow();
- GetWindowRect(calc_hwnd, &pos);
- GetWindowTextA(calc_hwnd, title, 1024);
- SetCursorPos(pos.left, pos.top);
- std::cout << pos.left << "," << pos.top << ","
- << pos.right << "," << pos.bottom << "\n" << title << "\n";
- std::cout << "Load data / save data? (L / S) : ";
- std::cin.getline(name, 1024);
- if (strcmp(name, "L") == 0) {
- std::cout << "File name: ";
- std::cin.getline(name, 1024);
- std::ifstream f (name);
- f >> n_keymap;
- for (int i = 0; i < n_keymap; i++) {
- f >> keymaps[i].pt.x >> keymaps[i].pt.y >> keymaps[i].vkey;
- }
- f.close();
- } else if (strcmp(name, "S") == 0) {
- std::cout << "File name: ";
- std::cin.getline(name, 1024);
- std::ofstream f (name);
- std::cout << "Enter number of key: ";
- std::cin >> n_keymap;
- f << n_keymap << "\n";
- while ((GetAsyncKeyState(VK_RETURN) & 0x8000) != 0) {}
- // ignore the last Enter (if any)
- std::cout << "Move cursor to position of each key and then press key.\n"
- "If mispressed key then click at this console window and press Delete button.\n";
- int j;
- for (int i = 0; i < n_keymap; i++) {
- std::cout << "Press key " << i << ": ";
- while (true) {
- Sleep(20);
- for(j = 0x08; j < 0xFF; j++) {
- if ((GetAsyncKeyState(j) & 0x8001) == 0x8001) {
- if (GetForegroundWindow() == calc_hwnd) { // explicit short circuit and &&
- keymaps[i].vkey = j;
- goto break_while1;
- } else if (j == VK_DELETE && i > 0) {
- i--;
- std::cout << "-DEL-\nPress key " << i << ": ";
- };
- }
- }
- } break_while1:
- GetKeyNameTextA(MapVirtualKeyA(j, MAPVK_VK_TO_VSC) << 16, name, 1024);
- GetCursorPos(&keymaps[i].pt);
- keymaps[i].pt.x -= pos.left;
- keymaps[i].pt.y -= pos.top;
- std::cout << j << " (" << name << ") at (" << keymaps[i].pt.x << ", " << keymaps[i].pt.y << ")\n";
- f << keymaps[i].pt.x << " " << keymaps[i].pt.y << " " << keymaps[i].vkey << "\n";
- }
- f.close();
- } else {
- std::cout << "Unrecognized\n";
- std::cin >> name;
- return 0;
- }
- INPUT inp {0};
- while (true) {
- Sleep(20);
- for(int i = 0; i < n_keymap; i++) {
- if ((GetAsyncKeyState(keymaps[i].vkey) & 0x8001) == 0x8001) {
- if (GetForegroundWindow() == calc_hwnd) {
- GetWindowRect(calc_hwnd, &pos);
- SetCursorPos(keymaps[i].pt.x + pos.left, keymaps[i].pt.y + pos.top);
- inp.type = INPUT_MOUSE;
- inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
- SendInput(1, &inp, sizeof(inp));
- inp.type = INPUT_MOUSE;
- inp.mi.dwFlags = MOUSEEVENTF_LEFTUP;
- SendInput(1, &inp, sizeof(inp));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement