如何将UCHAR虚拟密钥代码转换为std::string
How to convert UCHAR Virtual-Key Codes into std::string
我正在开发一个可以更改键盘设置的游戏选项。所以,我想显示玩家已经更改的密钥。我遇到了将包含虚拟密钥代码的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
中的三个问题:
1)您不会再删除lpszName
在返回之前生成delete lpszName;
,
或者首先使用静态缓冲区。
2)keyString = *lpszName;
只分配第一个字符,而不是整个字符串
删除*
。
3)GetKeyNameText
可能会失败。检查返回值,并检查GetLastError
(旁注:密钥名和256不会出现ERROR_INSUFFICIENT_BUFFER
)。
代码:
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);
}
相关文章:
- 将无符号char*转换为std::istream*C++
- 可以有效地转换 std::any 与 std::any_cast
- 尝试转换 std::chrono::d uration 会导致"rep cannot be a duration"编译错误
- 封送处理 - 转换 std::向量<char>到字符串^ 反之亦然
- 在 C++ 中转换 std::string,使其可以是 C# 中的 byte[]
- 转换 std::vector<T> 为 char *
- 转换std ::绑定到功能指针
- 强制转换 std::vector 的所有成员
- 无法转换'std::string{又名std::basic_string<char>}'到&
- 转换std::string到cv::Mat对象
- 转换std::chrono::system_clock::time_point::min()为字符串时,无效空指针错误
- 简单的模板化函数,用于转换 std::vectors - "illegal use of this type as an expression"
- Const转换std容器
- 如何强制转换std::make_unique,以便可以使用在类中声明的函数
- 如何向下转换std::shared_ptr
- 转换 std::vector<uint8_t> 为 QImage
- 使用std::dynamic_pointer_cast向上转换std::shared_ptr
- 如何转换std::vector<无符号字符>到向量<char>而无需复制
- 正在强制转换std::pair
const&T1 const, T2>const&安全的 - 如何在boost.python中转换std::string*