protected override void WndProc(ref Message message) { switch (message.Msg) { case Win32.WM_INPUT: { _keyboardDriver.ProcessRawInput(message.LParam); } break; case Win32.WM_USB_DEVICECHANGE: { Debug.WriteLine("USB Device Arrival / Removal"); _keyboardDriver.EnumerateDevices(); } break; } base.WndProc(ref message); } public void ProcessRawInput(IntPtr hdevice) { //Debug.WriteLine(_rawBuffer.data.keyboard.ToString()); //Debug.WriteLine(_rawBuffer.data.hid.ToString()); //Debug.WriteLine(_rawBuffer.header.ToString()); if (_deviceList.Count == 0) return; var dwSize = 0; Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref dwSize, Marshal.SizeOf(typeof(Rawinputheader))); if (dwSize != Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, out _rawBuffer, ref dwSize, Marshal.SizeOf(typeof(Rawinputheader)))) { Debug.WriteLine("Error getting the rawinput buffer"); return; } int virtualKey = _rawBuffer.data.keyboard.VKey; int makeCode = _rawBuffer.data.keyboard.Makecode; int flags = _rawBuffer.data.keyboard.Flags; if (virtualKey == Win32.KEYBOARD_OVERRUN_MAKE_CODE) return; var isE0BitSet = ((flags & Win32.RI_KEY_E0) != 0); KeyPressEvent keyPressEvent; if (_deviceList.ContainsKey(_rawBuffer.header.hDevice)) { lock (_padLock) { keyPressEvent = _deviceList[_rawBuffer.header.hDevice]; } } else { Debug.WriteLine("Handle: {0} was not in the device list.", _rawBuffer.header.hDevice); return; } var isBreakBitSet = ((flags & Win32.RI_KEY_BREAK) != 0); keyPressEvent.KeyPressState = isBreakBitSet ? "BREAK" : "MAKE"; keyPressEvent.Message = _rawBuffer.data.keyboard.Message; keyPressEvent.VKeyName = KeyMapper.GetKeyName(VirtualKeyCorrection(virtualKey, isE0BitSet, makeCode)).ToUpper(); keyPressEvent.VKey = virtualKey; if (KeyPressed != null) { KeyPressed(this, new RawInputEventArg(keyPressEvent)); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)