'when using KeyBoard hooks, how to filter control keys out
I have a keyboard hook in order to receive data from the HID interface. It works, however, sometimes, a binary data is received causing windows to behave incorrectly.
This is the hook delegate:
private IntPtr KeyboardHookDelegate(
int nCode, IntPtr wParam, IntPtr lParam)
{
EventHandler<KeyboardEventArgs> handlerKeyPressed = KeyBoardKeyPressed;
EventHandler<DataEventArgs> handlerData = KeyBoardDataPresent;
if (handlerKeyPressed != null || handlerData != null)
{
_shiftPressed = KbdNativeMethods.GetAsyncKeyState(KbdNativeMethods.VirtualKeyStates.VK_SHIFT) < 0;
_capsLockOn = KbdNativeMethods.GetAsyncKeyState(KbdNativeMethods.VirtualKeyStates.VK_CAPITAL) < 0;
if (nCode == KbdNativeMethods.HC_ACTION)
{
KbdNativeMethods.KBDLLHOOKSTRUCT kbd = (KbdNativeMethods.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KbdNativeMethods.KBDLLHOOKSTRUCT));
if ((wParam == (IntPtr)KbdNativeMethods.WM_SYSKEYDOWN) || (wParam == (IntPtr)KbdNativeMethods.WM_KEYDOWN))
{
if (kbd.vkCode == (uint)KbdNativeMethods.VirtualKeyStates.VK_LSHIFT || kbd.vkCode == (uint)KbdNativeMethods.VirtualKeyStates.VK_RSHIFT)
_shiftPressed = true;
else if (kbd.vkCode == (uint)KbdNativeMethods.VirtualKeyStates.VK_CAPITAL)
_capsLockOn = true;
else
{
char charPressed = GetCharFromKey(kbd.vkCode, _shiftPressed ^ _capsLockOn, false);
var isPrintable = (int)charPressed <= 0x7F && (!char.IsControl(charPressed) || char.IsWhiteSpace(charPressed));
if (isPrintable)
{
//Console.Write(string.Format("0x{0:X2} , ", (int)charPressed));
//if (charPressed != '\r')
// Console.Write(charPressed);
// Registra la última hora en que recibió un dato. Si ha pasado más de 3 segundos, debe limpiar el buffer.
DateTime newLastInput = GetLastInputTime();
if ((newLastInput - _lastInputTime).TotalSeconds >= 3)
_data.Clear();
_lastInputTime = newLastInput;
handlerKeyPressed?.Invoke(this, new KeyboardEventArgs() { Key = charPressed });
if (handlerData != null)
{
if (_data.Length > 500)
_data.Clear();
_data.Append(charPressed);
string tmp = _data.ToString();
if (tmp.StartsWith(_prefijo) && tmp.EndsWith(_sufijo))
{
_data.Clear();
string pin = ExtractPinFromReading(tmp.Substring(_prefijo.Length, tmp.Length - _prefijo.Length - _sufijo.Length).Trim());
if (pin != string.Empty)
handlerData(this, new DataEventArgs() { SourceName = "KBD", Data = pin });
else
return IntPtr.Zero;
}
}
}
else if (charPressed != 0x1B)
return IntPtr.Zero;
}
}
else if ((wParam == (IntPtr)KbdNativeMethods.WM_SYSKEYUP) || (wParam == (IntPtr)KbdNativeMethods.WM_KEYUP))
{
if (kbd.vkCode == (uint)KbdNativeMethods.VirtualKeyStates.VK_LSHIFT || kbd.vkCode == (uint)KbdNativeMethods.VirtualKeyStates.VK_RSHIFT)
_shiftPressed = false;
else if (kbd.vkCode == (uint)KbdNativeMethods.VirtualKeyStates.VK_CAPITAL)
_capsLockOn = false;
}
else
return IntPtr.Zero;
}
}
return KbdNativeMethods.CallNextHookEx(_keyBoardHandle, nCode, wParam, lParam);
}
As you can see, I am trying to filter those weird bytes out by returning IntPtr.Zero from the handler, but it did not work.
Any help please?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
