private RawInput ProcessRawInput(RawInput input, int outSize) { if (outSize != -1) { if (input.Header.Type == RawInputType.Keyboard) { if (Initializing) { var currentControl = currentForm.ActiveControl; controlByKeyboard[input.Header.Device] = currentControl; var initializationMessage = String.Format("{0} <- Keyboard[{1}]", currentControl.Name, input.Header.Device); MessageBox.Show(initializationMessage); } else { var msg = String.Format("[{0}] {1}", input.Header.Device, input.Data.Keyboard.VirtualKey.ToString()); if (controlByKeyboard.ContainsKey(input.Header.Device)) { Win32MinimalWrap.SendMessage(controlByKeyboard[input.Header.Device].Handle, (uint)WM.KEYDOWN, (IntPtr)input.Data.Keyboard.VirtualKey, IntPtr.Zero); } else { MessageBox.Show(msg); } } } } return(input); }
// Source: www.pinvoke.net // A convenient function for getting all raw input devices. // This method will get all devices, including virtual devices // For remote desktop and any other device driver that's registered // as such a device. public static RawInputDeviceList[] GetAllRawDevices() { uint deviceCount = 0; uint dwSize = (uint)Marshal.SizeOf(typeof(RawInputDeviceList)); // First call the system routine with a null pointer // for the array to get the size needed for the list uint retValue = Win32MinimalWrap.GetRawInputDeviceList(null, ref deviceCount, dwSize); // If anything but zero is returned, the call failed, so return a null list if (0 != retValue) { return(null); } // Now allocate an array of the specified number of entries var deviceList = new RawInputDeviceList[deviceCount]; // Now make the call again, using the array retValue = Win32MinimalWrap.GetRawInputDeviceList(deviceList, ref deviceCount, dwSize); // Free up the memory we first got the information into as // it is no longer needed, since the structures have been // copied to the deviceList array. //IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount)); //Marshal.FreeHGlobal(pRawInputDeviceList); // Finally, return the filled in list return(deviceList); }
public static void RegisterKeyboardRawInput() { var rid = new RawInputDevice[1]; rid[0].UsagePage = HIDUsagePage.Generic; rid[0].Usage = HIDUsage.Keyboard; rid[0].Flags = RawInputDeviceFlags.NoLegacy; rid[0].WindowHandle = IntPtr.Zero; if (Win32MinimalWrap.RegisterRawInputDevices(rid, rid.Length, Marshal.SizeOf(rid[0])) == false) { //registration failed. Call GetLastError for the cause of the error throw new Exception("Registration failed"); } }
public bool PreFilterMessage(ref Message m) { if ((WM)m.Msg == WM.INPUT) { var input = new RawInput(); int outSize = 0; int size = Marshal.SizeOf(typeof(RawInput)); outSize = Win32MinimalWrap.GetRawInputData(m.LParam, RawInputCommand.Input, out input, ref size, Marshal.SizeOf(typeof(RawInputHeader))); input = ProcessRawInput(input, outSize); Win32MinimalWrap.DispatchMessage(ref m); return(false); } return(false); }