Advertisement
alaestor

Hardcoded Key Remapper

Sep 4th, 2017
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | None | 0 0
  1. // Public Discord: http://public.FutureGadgetLab.net (Alaestor FGL 2017)
  2.  
  3. /*****************************************************************************
  4.  *   Copywrong (C) 1337-9001 Hooin Kyoma <Kyoma@FutureGadgetLab.fake>        *
  5.  *                                                                           *
  6.  *   This program is free software; you can redistribute it and/or modify    *
  7.  *   it under the terms of the GNU General Public License as published by    *
  8.  *   the Free Software Foundation; either version 9 of the License, or       *
  9.  *   (at your option) any license you want. I dun give fucks.                *
  10.  *                                                                           *
  11.  *   This program is distributed in the hope that it will be useful,         *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
  13.  *   LONGCAT IS LONG or FITNESS FOR A PATRIARCHAL PURPOSE.  See the          *
  14.  *   GNU General Public License for more dank memes.                         *
  15.  *                                                                           *
  16.  *   You shouldn't have received a copy of the GNU General Public License    *
  17.  *   with this program; if not, dont write to the Free Software Foundation   *
  18.  *   because you may be retarded. Think fast douche-fag.                     *
  19.  *                                                                           *
  20.  *   Divergence 1.048596, Future Gadget Lab, Chiyoda-ku KuramaeBashi Douri   *
  21.  *****************************************************************************/
  22.  
  23. /*
  24.     Simple "key remapper" program for windows.
  25.     Suppresses original key press if the key is remapped.
  26.  
  27.     Simulated keys were tested on both direct and raw input.
  28.     Must be run as admin to effect other admin-level processes.
  29. */
  30.  
  31. #define WINVER _WIN32_WINNT_WIN7
  32.  
  33. #include <cstdio>
  34. #include <string>
  35. #include <vector>
  36. #include <windows.h>
  37. #include <winuser.h> // should be redundent
  38.  
  39. // enable this for spam :D
  40. static constexpr bool debug_printing = false;
  41.  
  42. /**************************
  43.     printing functions
  44.  ***************************/
  45.  
  46. char cGetChar()
  47. {
  48.     // Get the first char of the user input
  49.     char first = getchar();
  50.  
  51.     // empty the buffer by reading from it until the end
  52.     register char c;
  53.     while ((c = getchar()) != '\n' && c != EOF);
  54.  
  55.     return first;
  56. }
  57.  
  58. void log(const std::string& msg)
  59. {
  60.     printf(" %s\n", msg.c_str());
  61. }
  62.  
  63. int error(const std::string& msg)
  64. {
  65.     printf(" Error: %s\n", msg.c_str());
  66.     puts("press ENTER to terminate");
  67.     cGetChar();
  68.     return 0;
  69. }
  70.  
  71. /**************************
  72.     managing objects
  73.  ***************************/
  74.  
  75. class Record
  76. {
  77.     public:
  78.     std::vector<unsigned char> in;
  79.     std::vector<unsigned char> out;
  80.  
  81.     bool pairCheck()
  82.     {
  83.         if (in.size() != out.size()) return error("Pair check failed!");
  84.         else return true;
  85.     }
  86.  
  87.     int findIndexFor(const unsigned char vkey_in)
  88.     {
  89.         const std::size_t elements = in.size();
  90.         for (unsigned int i = 0; i < elements; i++)
  91.             if (vkey_in == in[i]) return i;
  92.  
  93.         return (-1);
  94.     }
  95.  
  96.     void associate(
  97.         const unsigned char vkey_in,
  98.         const unsigned char vkey_out
  99.     )
  100.     {
  101.         in.push_back(vkey_in);
  102.         out.push_back(vkey_out);
  103.     }
  104.  
  105.     bool disassociate(const unsigned char vkey_in)
  106.     {
  107.         const int i = findIndexFor(vkey_in);
  108.         if (i < 0) return error("disassociation index not found!");
  109.  
  110.         in.erase(in.begin() + i);
  111.         out.erase(out.begin() + i);
  112.  
  113.         return true;
  114.     }
  115.  
  116.     Record()
  117.     {
  118.         constexpr std::size_t reserved_elements = 10;
  119.         in.reserve(reserved_elements);
  120.         out.reserve(reserved_elements);
  121.     }
  122.     ~Record() = default;
  123. };
  124.  
  125.  
  126. static class KeyEventManager
  127. {
  128.     private:
  129.     Record key;
  130.  
  131.     public:
  132.     auto output(const unsigned char vkey, const bool downPress)
  133.     { // simulate keyboard input
  134.         if (debug_printing)
  135.         {
  136.             char buff[50];
  137.             snprintf(buff, sizeof(buff),
  138.                 "output:\tvkey %02x %s (from remap)",
  139.                 vkey, downPress ? "down" : "up"
  140.             );
  141.             log(buff);
  142.         }
  143.  
  144.         INPUT in;
  145.         in.type = INPUT_KEYBOARD;
  146.         in.ki.wVk = vkey;
  147.         in.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC);
  148.         in.ki.dwFlags = downPress ? 0 : KEYEVENTF_KEYUP;
  149.         in.ki.dwExtraInfo = 0;
  150.         in.ki.time = 0;
  151.  
  152.         return SendInput(1, &in, sizeof(in));
  153.     }
  154.  
  155.     bool input(const unsigned char vkey, const bool downPress)
  156.     { // input behavior
  157.         const int i = key.findIndexFor(vkey);
  158.         if (i < 0)
  159.         {
  160.             if (debug_printing)
  161.             {
  162.                 char buff[50];
  163.                 snprintf(buff, sizeof(buff),
  164.                     "input: \tvkey %02x %s",
  165.                     vkey, downPress ? "down" : "up"
  166.                 );
  167.                 log(buff);
  168.             }
  169.  
  170.             return false;
  171.         }
  172.         else if (debug_printing)
  173.         {
  174.             char buff[50];
  175.             snprintf(buff, sizeof(buff),
  176.                 "input: \tvkey %02x %s (remapped)",
  177.                 vkey, downPress ? "down" : "up"
  178.             );
  179.             log(buff);
  180.         }
  181.  
  182.         output(key.out[i], downPress);
  183.         return true;
  184.     }
  185.  
  186.     void remap(
  187.         const unsigned char vkey_in,
  188.         const unsigned char vkey_out
  189.     )
  190.     { // make a remap, associate a key in with a key out
  191.         if (debug_printing)
  192.         {
  193.             char buff[50];
  194.             snprintf(buff, sizeof(buff),
  195.                 "Remap created: vkey %02x to %02x",
  196.                 vkey_in, vkey_out
  197.             );
  198.             log(buff);
  199.         }
  200.  
  201.         key.associate(vkey_in, vkey_out);
  202.     }
  203.  
  204.     bool remapDelete(const unsigned char vkey)
  205.     { // destroy a remap, disassociate a key in with a key out
  206.         if (debug_printing)
  207.         {
  208.             char buff[50];
  209.             snprintf(buff, sizeof(buff),
  210.                 "Remap deleted: vkey %02x", vkey);
  211.             log(buff);
  212.         }
  213.  
  214.         return key.disassociate(vkey);
  215.     }
  216.  
  217.     KeyEventManager() = default;
  218.     ~KeyEventManager() = default;
  219.  
  220. } keyEvent;
  221.  
  222. /**************************
  223.     windows callback
  224.  ***************************/
  225.  
  226. LRESULT CALLBACK llKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
  227. {
  228.     if (nCode == HC_ACTION)
  229.     {
  230.         KBDLLHOOKSTRUCT& kb = *reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
  231.         const unsigned char vkey = kb.vkCode;
  232.         const bool state = (wParam == WM_KEYDOWN);
  233.  
  234.         const bool remapped = keyEvent.input(vkey, state);
  235.         if (remapped) return -1; // supress original input
  236.     }
  237.  
  238.     return CallNextHookEx(nullptr, nCode, wParam, lParam);
  239. }
  240.  
  241. /**************************
  242.     Proc elevation check
  243.  ***************************/
  244.  
  245. bool elevated(HANDLE process)
  246. {
  247.     HANDLE h = INVALID_HANDLE_VALUE;
  248.     TOKEN_ELEVATION te;
  249.  
  250.     if (OpenProcessToken(process, TOKEN_QUERY, &h))
  251.     {
  252.         unsigned long cbSize = sizeof(TOKEN_ELEVATION);
  253.         GetTokenInformation(h, TokenElevation, &te, sizeof(te), &cbSize);
  254.         if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
  255.     }
  256.  
  257.     return te.TokenIsElevated ? true : false;
  258. }
  259.  
  260. /**************************
  261.     Primary Functions
  262.  ***************************/
  263.  
  264. void remap_keys()
  265. { // specify virtual key remaps here
  266.     keyEvent.remap(VK_F20, VK_NUMPAD7);
  267.     keyEvent.remap(VK_F21, VK_NUMPAD8);
  268.     keyEvent.remap(VK_F24, VK_NUMPAD9);
  269. }
  270.  
  271. int main()
  272. {
  273.     // make sure we're running as admin, because we should be
  274.     if ( !elevated(GetCurrentProcess()) )
  275.         return error("Key Remapper must be run as administrator");
  276.  
  277.     log("Hardcoded Key Remapper STARTED");
  278.     remap_keys();
  279.  
  280.     HHOOK keyboardHook = SetWindowsHookEx(
  281.         WH_KEYBOARD_LL,
  282.         llKeyboardProc,
  283.         GetModuleHandle("kernel32.dll"),
  284.         0
  285.     );
  286.     if (keyboardHook==nullptr) return error("SetWindowsHook() failed");
  287.  
  288.     MSG msg;
  289.     while ( !GetMessage(&msg, nullptr, 0, 0))
  290.     {
  291.         TranslateMessage(&msg);
  292.         DispatchMessage(&msg);
  293.     }
  294.  
  295.     UnhookWindowsHookEx(keyboardHook);
  296.  
  297.     log("Hardcoded Key Remapper ENDED");
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement