title: VC++ : Enumerate the list of virtual key codes along with scan codes for all the possible key combinations
body: <p>I would like to enumerate the list of all possible key combinations supported by the current keyboard layout (with virtual key codes, scan codes and their Unicode values). To map the remote user inputs to keys in order to simulate them.</p>
  
  <p>I was expecting an API like <a href="https://developer.apple.com/documentation/coreservices/1390584-uckeytranslate?language=objc" rel="nofollow noreferrer">UCKeyTranslate</a>(ObjectiveC) for VC++ that can accept the virtual key codes and modifiers(ALT, SHIFT, CTRL) and provide me the scan codes, but couldn't find anything similar to that.</p>
  
  <p>After a lot of research and spending 2 whole days, I had no other option to go with except for MapVirtualKeyEx. </p>
  
  <p>I came with the following code but that has a lot of problems,</p>
  
  <pre><code>BOOL PopulateKeyMap()
  {
      TCHAR Buff[120 * sizeof(TCHAR)] = { 0 };
      GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, Buff, sizeof(Buff));
      HKL hKeyboardLayout = ::LoadKeyboardLayout(Buff, KLF_ACTIVATE);
  
      {
          lock_guard&lt;recursive_mutex&gt; lockHolder(cs_populate_key);
  
          if (hCurrentKeyboardLayout)
          {
              UnloadKeyboardLayout(hCurrentKeyboardLayout);
          }
  
          hCurrentKeyboardLayout = hKeyboardLayout;
  
          // Prepopulate keyCodeDictionary with common key combinations.
          for (int keyIndex = 0; keyIndex &lt; KEY_CODES_DICT_SIZE; ++keyIndex)
          {
              {
                  unsigned int Vk;
  
                  tstring key_name = GetKeyName(keyIndex, Vk);
  
                  if (key_name.compare(_T("")) != 0)
                  {
                      SmartPtr&lt;KeyCodeInfo&gt; key = (new KeyCodeInfo);
  
                      if (key)
                      {
                          key-&gt;nIndex = keyIndex;
                          key-&gt;sVKCode = keyIndex;
                          key-&gt;nScanCode = Vk;
  
                          keyboard_map[key_name] = key;
                      }
                  }// End if
              }
          }// End for
  
          bKeyMapInitialized = TRUE;
      }
  
      return TRUE;
  }
  
  tstring GetKeyName(unsigned int virtualKey, unsigned int &amp;scanCode)
  {
      if (!hCurrentKeyboardLayout)
      {
          PopulateKeyMap();
      }
  
      scanCode = MapVirtualKeyEx(virtualKey, MAPVK_VK_TO_VSC, hCurrentKeyboardLayout);
  
      // because MapVirtualKey strips the extended bit for some keys
      switch (virtualKey)
      {
          case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: // arrow keys
          case VK_PRIOR: case VK_NEXT: // page up and page down
          case VK_END: case VK_HOME:
          case VK_INSERT: case VK_DELETE:
          case VK_DIVIDE: // numpad slash
          case VK_NUMLOCK:
          {
              scanCode |= KF_EXTENDED; // set extended bit
              break;
          }
      }
  
      TCHAR keyName[256];
      if (GetKeyNameText(scanCode &lt;&lt; 16, keyName, sizeof(keyName)) != 0)
      {
          return keyName;
      }
      else
      {
          return _T("");
      }
  }
  </code></pre>
  
  <p>The MapVirtualKeyEx provides me only the list of basic scan codes and not the scan codes of the keys with the combinations of the modifiers (ALT, CTRL, SHIFT). Is there any way I can provide the combinations of the modifiers as input to the function so that I can generate the required key combinations?.</p>
  
  <p>Any help would be appreciated. Thanks in advance.</p>