title: How to convert UCHAR Virtual-Key Codes into std::string
body: <p>I'm working on a game option that change the keyboard setting. So, I want to display the key that player has changed. I have the issue to convert UCHAR key that contains a Virtual-Key Codes into std::string. Seem that GetKeyNameText() only cannot convert UCHAR into string as no string has been displayed on screen. How can I fix this issue? Thanks.</p>
  
  <p>Get keyboard message</p>
  
  <pre><code>LRESULT Game::messageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      if (initialized)    // do not process messages if not initialized
      {
          switch (msg)
          {
           case WM_KEYDOWN: case WM_SYSKEYDOWN:       // key down
              input-&gt;keyDown(wParam);
              return 0;
          }
      }
      return DefWindowProc(hwnd, msg, wParam, lParam);    // let Windows handle it
  }
  </code></pre>
  
  <p>Input class</p>
  
  <pre><code>UCHAR key;  
  
  void Input::keyDown(WPARAM wParam)
  {
      // make sure key code is within buffer range
      if (wParam &lt; inputNS::KEYS_ARRAY_LEN)
      {
          keysDown[wParam] = true;    // update keysDown array
          // key has been "pressed, erased by clear()
          keysPressed[wParam] = true; // update keysPressed array
          key = wParam;
      }
  }
  
  UCHAR getKeyPressed() { return key; }
  </code></pre>
  
  <p>Game class</p>
  
  <p>Note: Code detail has been skipped. </p>
  
  <pre><code>// Key control
  UCHAR vkeyUp;
  UCHAR vkeyDown;
  
  void GameContent::update()
  {
      if (buttonX == 220)
      {
          if (buttonY == 15)
          {
              if (input-&gt;anyKeyPressed()) {
                  vkeyUp = input-&gt;getKeyPressed();
                  buttonX = 10;
                  buttonY = 15;
              }   
          }
          else if (buttonY == 65)
          {
              if (input-&gt;anyKeyPressed()) {
                  vkeyDown = input-&gt;getKeyPressed();
                  buttonX = 10;
                  buttonY = 65;
              }
          }
      }
  
      button.setX(buttonX);
      button.setY(buttonY);
  }
  
  
  void GameContent::render()
  {
      font.print("Move Up", 20, 20);      font.print(input-&gt;getKeyPressedString(vkeyUp), 300, 20);
      font.print("Move Down", 20, 70);    font.print(input-&gt;getKeyPressedString(vkeyDown), 300, 70);
  }
  
  
  std::string Input::getKeyPressedString(UCHAR vkey)
  {
      std::string keyString;
      TCHAR *lpszName = new TCHAR[256];
      GetKeyNameText(vkey, lpszName, sizeof(lpszName));
  
      keyString = *lpszName;
      return keyString;
  }
  </code></pre>
  
  <p>DirectX Font Class</p>
  
  <pre><code>int TextDX::print(const std::string &amp;str, int x, int y)
  {
      if (dxFont == NULL)
          return 0;
      // Set font position
      fontRect.top = y;
      fontRect.left = x;
  
      // Rotation center
      D3DXVECTOR2 rCenter = D3DXVECTOR2((float)x, (float)y);
      // Setup matrix to rotate text by angle
      D3DXMatrixTransformation2D(&amp;matrix, NULL, 0.0f, NULL, &amp;rCenter, angle, NULL);
      // Apply Matrix
      graphics-&gt;getSprite()-&gt;SetTransform(&amp;matrix);
      return dxFont-&gt;DrawTextA(graphics-&gt;getSprite(), str.c_str(), -1, &amp;fontRect, DT_LEFT, color);
  }
  </code></pre>