【问题标题】:How to convert UCHAR Virtual-Key Codes into std::string [duplicate]如何将 UCHAR 虚拟键代码转换为 std::string [重复]
【发布时间】:2014-02-22 19:52:54
【问题描述】:

我正在开发一个可以更改键盘设置的游戏选项。所以,我想显示玩家改变的键。我有将包含虚拟键代码的 UCHAR 键转换为 std::string 的问题。似乎 GetKeyNameText() 只能将 UCHAR 转换为字符串,因为屏幕上没有显示任何字符串。我该如何解决这个问题?谢谢。

获取键盘消息

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->keyDown(wParam);
            return 0;
        }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);    // let Windows handle it
}

输入类

UCHAR key;  

void Input::keyDown(WPARAM wParam)
{
    // make sure key code is within buffer range
    if (wParam < 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; }

游戏类

注意:已跳过代码详细信息。

// Key control
UCHAR vkeyUp;
UCHAR vkeyDown;

void GameContent::update()
{
    if (buttonX == 220)
    {
        if (buttonY == 15)
        {
            if (input->anyKeyPressed()) {
                vkeyUp = input->getKeyPressed();
                buttonX = 10;
                buttonY = 15;
            }   
        }
        else if (buttonY == 65)
        {
            if (input->anyKeyPressed()) {
                vkeyDown = input->getKeyPressed();
                buttonX = 10;
                buttonY = 65;
            }
        }
    }

    button.setX(buttonX);
    button.setY(buttonY);
}


void GameContent::render()
{
    font.print("Move Up", 20, 20);      font.print(input->getKeyPressedString(vkeyUp), 300, 20);
    font.print("Move Down", 20, 70);    font.print(input->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;
}

DirectX 字体类

int TextDX::print(const std::string &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(&matrix, NULL, 0.0f, NULL, &rCenter, angle, NULL);
    // Apply Matrix
    graphics->getSprite()->SetTransform(&matrix);
    return dxFont->DrawTextA(graphics->getSprite(), str.c_str(), -1, &fontRect, DT_LEFT, color);
}

【问题讨论】:

  • 直到现在才检查 getKeyPressedString,但是:a) 为什么* keyString = *lpszName; 这将只复制一个字符... b) 你没有释放 lpszName。 c)您不检查 GetKeyNameText 的返回值
  • a) 更改为 keyString = lpszName; ? b) 使用免费(lpszName); ? c) 如何查看GetKeyNameText的返回值?
  • a) 是的。 b) 使用 delete lpszName; 而不是 free,因为您使用的是 new 而不是 malloc。
  • c) 如何查看GetKeyNameText的返回值?
  • 使用谷歌。 msdn.microsoft.com/en-us/library/windows/desktop/… 描述了发生错误时的值。 ...我会和大家一起回答...

标签: c++ windows winapi input directx


【解决方案1】:

getKeyPressedString中的三个问题:
1) 你不会再次删除lpszName
在返回之前创建delete lpszName;
或首先使用静态缓冲区。

2) keyString = *lpszName; 只分配第一个字符,而不是整个字符串。
删除*

3) GetKeyNameText 可能会失败。检查返回值,还有GetLastError
(旁注:ERROR_INSUFFICIENT_BUFFER 不会发生在键名和 256 上)。

代码:

std::string Input::getKeyPressedString(UCHAR vkey)
{
    TCHAR lpszName[256];
    if(!GetKeyNameText(vkey, lpszName, sizeof(lpszName)))
    {
        //maybe? Or throw std::systemerror(GetLastError(), std::system_category())
        return std::string("");
    }
    return std::string(lpszName);
}

【讨论】:

  • 还是不行。
  • 编译器错误(消息?),运行时崩溃(消息?行,在调试时发现?),不良行为(您期望什么以及发生了什么),...?
  • 程序运行正常,没有任何问题,但我无法在屏幕上显示按键。
  • 那程序运行得怎么样了? :facepalm: ...对不起,我不知道发生了什么,我帮不了你
  • 在此之前,我将返回字符串一一定义。 i453.photobucket.com/albums/qq253/ulti-killer/… 但在我应用你的代码后,它不再显示密钥。 i453.photobucket.com/albums/qq253/ulti-killer/… 现在,我为显示文本添加了directx 字体。
猜你喜欢
  • 2018-09-20
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 2013-05-11
  • 2013-09-10
  • 2012-10-31
  • 2014-09-08
  • 1970-01-01
相关资源
最近更新 更多