在Windows API和Direct2D/DirectWrite中,我正在檢測虛擬代碼,以便可以附加2D GUI中的文本輸入。雖然它可以正常工作,但是如何包含非字母,例如!?等。如何使用Windows API中的虛擬代碼檢測非字母?
例如,當我按下Shift + 1時,我得到'1'而不是'!'。當我按'。'時,我得到一個盒裝角色。該功能可以在某種程度上檢查此檢測嗎?
wchar_t TextBox::charIsPressed(int getKey)
{
char letter = getKey;
// Check for space character
if (letter == ' ')
return (wchar_t)letter;
// Check if the input is no letter
if ((getKey >= 'A') && (getKey <= 'Z'))
{
if (!GetAsyncKeyState(VK_SHIFT))
letter += 0x20;
}
return (wchar_t)letter;
}
它調用的函數:
// Keyboard support
static X2D::Win32::KeyEvent *keyEvent;
if (m_focused)
{
// Check if there's editing space
if ((m_x + m_text.getWidth()) > (m_x + getWidth()))
return;
// Get the latest key event
keyEvent = frm.getKeyEvent();
if (!keyEvent->processed)
{
// Was backspace pressed?
if (keyEvent->virtual_code == VK_BACK)
{
m_text.setText(m_text.getText().substr(0, m_text.getText().length() - 1));
}
else if (keyEvent->virtual_code == VK_RETURN)
{
m_focused = false;
}
else
{
m_text.setText(m_text.getText() + charIsPressed(keyEvent->virtual_code));
}
keyEvent->processed = true;
}
}
編輯: 我發現用於檢測單個字符的方式,所以它是一個開始。
// Converts '1' to '!'
if (getKey == '1')
{
if (GetAsyncKeyState(VK_SHIFT))
return '!';
}
雖然鍵入'。'讓我變成一個半雪人Ascii的身影。
你爲什麼不讓系統爲你做繁重的任務?只需處理'WM_CHAR',一切正常。無需手動編寫輸入即可完成系統已爲您執行的操作。 – IInspectable