我正在使用全局键盘挂钩来使用条形码阅读器。条码阅读器在条码前面发送一个 STX 字符,在条码后面发送一个 ETX 字符。
有时 ToAscii 函数会产生正确的 STX 和 ETX 代码(0x02 或 0x03),但大多数时候它会变为 0x62 (b) 或 0x63 (c)。
这可以解释并最好解决吗?
为了清楚起见,我在下面添加了钩子回调:
private IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
{
if (nCode >= 0)
{
// Prepare the characters and retrieve the keyboard state.
char[] characters = new char[2];
byte[] keyState = GetKeyboardState();
if (KeyPressed != null && WinAPI.ToAscii(lParam.vkCode, lParam.scanCode, keyState, characters, 0) == 1)
{
// Initialize the event arguments and fire the KeyPressed event.
GlobalKeyboardHookEventArgs e = new GlobalKeyboardHookEventArgs(characters, (int)wParam);
KeyPressed(null, e);
// Do not call the next hook if the event has been handled.
if (e.Handled)
{
return (IntPtr)1;
}
}
}
// Call the next hook.
return WinAPI.CallNextHookEx(hook, nCode, wParam, ref lParam);
}
}