在Windows API和Direct2D/DirectWrite中,我正在检测虚拟代码,因此可以附加2D GUI中的文本输入。虽然它工作正常,但我如何包含非字母,例如?.,等。
例如,当我按 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 人物。
尝试这样的事情(这是德尔菲,但它可以让您看到翻译的原理(:
function VKToChar(AVirtualCode: Word; out AChar: WideChar): Boolean;
var
KeyboardState: TKeyboardState;
ScanCode: DWORD;
Temp: UnicodeString;
Char: WideChar;
begin
AChar := #0;
Result := GetKeyboardState(KeyboardState);
if not Result then Exit;
ScanCode := MapVirtualKey(AVirtualCode, MAPVK_VK_TO_VSC);
SetLength(Temp, 3);
if ToUnicode(AVirtualCode, ScanCode, KeyboardState, PWideChar(Temp), Length(Temp), 0) = 1 then
begin
AChar := Temp[1];
Result := True;
end
else
Result := False;
end;