我有一个需要监听扫描仪QRcode内容的应用程序,QRcode的内容包含ASCII和UTF-8字符。我使用以下代码,但它只能接收ASCII字符。所有UTF-8字符都消失。如何接收UTF-8字符?
听按键输入
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if ((nCode = 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null))
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam,
typeof(KeyboardHookStruct));
if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
System.Windows.Forms.KeyEventArgs e = new System.Windows.Forms.KeyEventArgs(keyData);
KeyDownEvent(this, e);
}
if (KeyPressEvent != null && wParam == WM_KEYDOWN)
{
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
byte[] inBuffer = new byte[2];
if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer,
MyKeyboardHookStruct.flags) == 1)
{
KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
KeyPressEvent(this, e);
}
}
if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
System.Windows.Forms.KeyEventArgs e = new System.Windows.Forms.KeyEventArgs(keyData);
KeyUpEvent(this, e);
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
处理键
public void Add(EventMsg msg)
{
if (_keys.Count == 0)
{
_keys.Add(new List<EventMsg());
_keys[0].Add(msg);
_result.Add(string.Empty);
}
else if (_keydown.Count 0)
{
_keys[_keys.Count - 1].Add(msg);
}
else if (((TimeSpan)(DateTime.Now - _last)).TotalMilliseconds < ts)
{
_keys[_keys.Count - 1].Add(msg);
}
else
{
_keys.Add(new List<EventMsg());
_keys[_keys.Count - 1].Add(msg);
_result.Add(string.Empty);
}
_last = DateTime.Now;
if (msg.paramH == 0 && !_keydown.Contains(msg.message))
{
_keydown.Add(msg.message);
}
if (msg.paramH 0 && _keydown.Contains(msg.message))
{
_keydown.Remove(msg.message);
}
int v = msg.message & 0xff;
int c = msg.paramL & 0xff;
StringBuilder strKeyName = new StringBuilder(500);
if (GetKeyNameText(c * 65536, strKeyName, 255) 0)
{
_key = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
GetKeyboardState(_state);
if (_key.Length == 1 && msg.paramH == 0)
{
_cur = ShiftChar(_key, isShift, _state).ToString();
_result[_result.Count - 1] += _cur;
}
else if (_key.Length == 5 && msg.paramH == 0 && msg.paramL == 78 && msg.message == 107)
{
_cur = Convert.ToChar('+').ToString();
_result[_result.Count - 1] += _cur;
}
else
{
_cur = string.Empty;
}
}
}
private char ShiftChar(string k, bool isShiftDown, byte[] state)
{
bool capslock = state[0x14] == 1;
bool numlock = state[0x90] == 1;
bool scrolllock = state[0x91] == 1;
bool shiftdown = state[0xa0] == 1;
char chr = (capslock ? k.ToUpper() : k.ToLower()).ToCharArray()[0];
if (isShiftDown)
{
if (chr = 'a' && chr <= 'z')
{
chr = (char)((int)chr - 32);
}
else if (chr = 'A' && chr <= 'Z')
{
if (chr == 'Z')
{
string s = "";
}
chr = (char)((int)chr + 32);
}
else
{
string s = "`1234567890-=[];',./";
string u = "~!@#$%^&*()_+{}:\"<?";
if (s.IndexOf(chr) = 0)
{
return (u.ToCharArray())[s.IndexOf(chr)];
}
}
}
return chr;
}
}
在add()中,将记录所有ASCII字符,并丢弃其他字符,例如确保它支持其他字符集,例如utf-8?