Advertisement
asqapro

KeyConfig

Dec 23rd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <windows.h>
  5. #include <winable.h>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. vector<int> to_swap_keycodes; //make it global so the hook can access it
  11. vector<int> swap_to_keycodes; //holds the int versions of the letters the user is looking to swap
  12.  
  13. HHOOK hHock = NULL;
  14.  
  15. void GenerateKey(int vk, bool event) { //generates keys
  16.  
  17.     KEYBDINPUT  kb = {0};
  18.     INPUT       Input = {0};
  19.     if(!event){
  20.         /* Generate a "key down" */
  21.         kb.wVk  = vk;
  22.         Input.type  = INPUT_KEYBOARD;
  23.         Input.ki  = kb;
  24.         SendInput(1, &Input, sizeof(Input));
  25.     }
  26.     else{
  27.         /* Generate a "key up" */
  28.         ZeroMemory(&kb, sizeof(KEYBDINPUT));
  29.         ZeroMemory(&Input, sizeof(INPUT));
  30.         kb.dwFlags  =  KEYEVENTF_KEYUP;
  31.         kb.wVk = vk;
  32.         Input.type = INPUT_KEYBOARD;
  33.         Input.ki = kb;
  34.         SendInput(1, &Input, sizeof(Input));
  35.     }
  36. }
  37.  
  38. LRESULT CALLBACK MyLowLevelHook(int nCode, WPARAM wParam, LPARAM lParam)
  39. {
  40.     KBDLLHOOKSTRUCT* pkbhs = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam); //convert lParam to something usable
  41.     unsigned int index = find(to_swap_keycodes.begin(), to_swap_keycodes.end(), pkbhs->vkCode) - to_swap_keycodes.begin(); //find where the pressed key is in the vector
  42.     if(index < to_swap_keycodes.size()){ //if the keycode is one to be swapped
  43.         if(wParam == WM_KEYDOWN){ //if the key was pressed
  44.             GenerateKey(swap_to_keycodes[index], false); //generate a key press
  45.         }
  46.         else if(wParam == WM_KEYUP){ //if it was released
  47.             GenerateKey(swap_to_keycodes[index], true); //generate a key release
  48.         }
  49.         return 1; //don't call the next hook, stopping the original key from being sent
  50.     }
  51.     return CallNextHookEx(hHock, nCode, wParam, lParam); //otherwise, call the next hook in the chain
  52. }
  53.  
  54. void key_swap(vector<string> to_swap, vector<string> swap_to){ //holds the hook loop and key swap code
  55.     map<string, int> keycodes; //a map for keys to keycodes. Incomplete because I need shorter names for some keys
  56.     keycodes.insert(make_pair("a", 0x41));
  57.     keycodes.insert(make_pair("b", 0x42));
  58.     keycodes.insert(make_pair("c", 0x43));
  59.     keycodes.insert(make_pair("d", 0x44));
  60.     keycodes.insert(make_pair("e", 0x45));
  61.     keycodes.insert(make_pair("f", 0x46));
  62.     keycodes.insert(make_pair("g", 0x47));
  63.     keycodes.insert(make_pair("h", 0x48));
  64.     keycodes.insert(make_pair("i", 0x49));
  65.     keycodes.insert(make_pair("j", 0x4A));
  66.     keycodes.insert(make_pair("k", 0x4B));
  67.     keycodes.insert(make_pair("l", 0x4C));
  68.     keycodes.insert(make_pair("m", 0x4D));
  69.     keycodes.insert(make_pair("n", 0x4E));
  70.     keycodes.insert(make_pair("o", 0x4F));
  71.     keycodes.insert(make_pair("p", 0x50));
  72.     keycodes.insert(make_pair("q", 0x51));
  73.     keycodes.insert(make_pair("r", 0x52));
  74.     keycodes.insert(make_pair("s", 0x53));
  75.     keycodes.insert(make_pair("t", 0x54));
  76.     keycodes.insert(make_pair("u", 0x55));
  77.     keycodes.insert(make_pair("v", 0x56));
  78.     keycodes.insert(make_pair("w", 0x57));
  79.     keycodes.insert(make_pair("x", 0x58));
  80.     keycodes.insert(make_pair("y", 0x59));
  81.     keycodes.insert(make_pair("z", 0x5A));
  82.     keycodes.insert(make_pair("0", 0x30));
  83.     keycodes.insert(make_pair("1", 0x31));
  84.     keycodes.insert(make_pair("2", 0x32));
  85.     keycodes.insert(make_pair("3", 0x33));
  86.     keycodes.insert(make_pair("4", 0x34));
  87.     keycodes.insert(make_pair("5", 0x35));
  88.     keycodes.insert(make_pair("6", 0x36));
  89.     keycodes.insert(make_pair("7", 0x37));
  90.     keycodes.insert(make_pair("8", 0x38));
  91.     keycodes.insert(make_pair("9", 0x39));
  92.     keycodes.insert(make_pair("left", 0x25));
  93.     keycodes.insert(make_pair("up", 0x26));
  94.     keycodes.insert(make_pair("right", 0x27));
  95.     keycodes.insert(make_pair("down", 0x28));
  96.     for(unsigned int iter = 0; iter < to_swap.size(); iter++){ //generate the keycode vectors
  97.         to_swap_keycodes.push_back(keycodes.find(to_swap[iter])->second);
  98.         swap_to_keycodes.push_back(keycodes.find(swap_to[iter])->second);
  99.     }
  100.     while(true){ //keyboard hook look
  101.         MSG msg;
  102.         hHock = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelHook , NULL,NULL);
  103.  
  104.         while(GetMessage(&msg, NULL, NULL, NULL)){
  105.             TranslateMessage(&msg);
  106.             DispatchMessage(&msg);
  107.         }
  108.  
  109.         UnhookWindowsHookEx(hHock);
  110.     }
  111. }
  112.  
  113. struct RemoveDelimiter //removes whitespace from strings. Allows for more flexible input-handling
  114. {
  115.   bool operator()(char c)
  116.   {
  117.     return (c =='\r' || c =='\t' || c == ' ' || c == '\n');
  118.   }
  119. };
  120.  
  121. int main()
  122. {
  123.     cout << "Note that that functionality is limited to A-Z and 0-9 non-numberpad" << endl;
  124.     cout << "Format is \'Normal Key : Swapped Key\'" << endl;
  125.     cout << "Enter \'end\' to finish." << endl;
  126.     string entry;
  127.     vector<string> temp_to_swap;
  128.     vector<string> temp_swap_to;
  129.     while(true){
  130.         cout << "Entry: ";
  131.         getline(cin, entry); //get the two keys to be swapped
  132.         entry.erase(remove_if(entry.begin(), entry.end(), RemoveDelimiter()), entry.end()); //clean the input
  133.         temp_to_swap.push_back(entry.substr(0, entry.find(":"))); //get the to-be-swapped
  134.         temp_swap_to.push_back(entry.substr(entry.find(":")+1)); //get the swap-to
  135.         if(entry == "end" || entry == "End" || entry == "END"){ //if the user is done entering
  136.             key_swap(temp_to_swap, temp_swap_to); //create the hook and swap keys
  137.             break;
  138.         }
  139.         cin.clear(); cin.sync();
  140.     }
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement