Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将UCHAR虚拟密钥代码转换为std::string_C++_Windows_Winapi_Input_Directx - Fatal编程技术网

C++ 如何将UCHAR虚拟密钥代码转换为std::string

C++ 如何将UCHAR虚拟密钥代码转换为std::string,c++,windows,winapi,input,directx,C++,Windows,Winapi,Input,Directx,我正在做一个改变键盘设置的游戏选项。因此,我想显示播放器已更改的键。我遇到了将包含虚拟密钥代码的UCHAR密钥转换为std::string的问题。似乎GetKeyNameText only无法将UCHAR转换为字符串,因为屏幕上没有显示任何字符串。如何解决此问题?谢谢 获取键盘消息 LRESULT Game::messageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (initialized) //

我正在做一个改变键盘设置的游戏选项。因此,我想显示播放器已更改的键。我遇到了将包含虚拟密钥代码的UCHAR密钥转换为std::string的问题。似乎GetKeyNameText only无法将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; }
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中有三个问题: 1您不能再次删除lpszName。 要么删除lpszName;回来之前,, 或者首先使用静态缓冲区

2键串=*lpszName;仅指定第一个字符,而不是整个字符串。 删除*

3 GetKeyNameText可能会失败。检查返回值,并检查GetLastError。 旁注:错误\u缓冲区不足\u不会发生键名和256

代码:


直到现在才检查getKeyPressedString,但是:a Why*at keyString=*lpszName;这将只复制一个字符。。。b你不知道我的名字。c您不检查GetKeyNameTexta的返回值更改为keyString=lpszName?b使用freelpszName?c如何检查GetKeyNameText的返回值?a是。b使用删除lpszName;而不是免费的,因为您使用的是new而不是malloc.c。如何检查GetKeyNameText的返回值?使用google。描述发生错误时的值。。。我会用所有的答案回答…它仍然不起作用。编译器错误消息?,运行时崩溃消息?行,每次调试时发现?不必要的行为您期望发生什么以及发生了什么…?程序运行正常,没有任何问题,但我无法在屏幕上显示按键。那么程序如何运行正常脸掌:。。。很抱歉,在不知道发生了什么之前,我无法帮助您。在此之前,我逐个定义了返回字符串。但在我应用ur代码后,它不再显示键。现在,我为显示文本添加了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);
}
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);
}