public RawKeyboard(IntPtr hwnd, bool captureOnlyInForeground) { var rid = new RawInputDevice[1]; rid[0].UsagePage = HidUsagePage.GENERIC; rid[0].Usage = HidUsage.Keyboard; rid[0].Flags = (captureOnlyInForeground ? RawInputDeviceFlags.NONE : RawInputDeviceFlags.INPUTSINK) | RawInputDeviceFlags.DEVNOTIFY; rid[0].Target = hwnd; if (!Win32.RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0]))) { throw new ApplicationException("Failed to register raw input device(s)."); } }
public RawKeyboard(IntPtr hwnd, bool captureOnlyInForeground) { var rid = new RawInputDevice[1]; rid[0].UsagePage = HidUsagePage.GENERIC; rid[0].Usage = HidUsage.Keyboard; rid[0].Flags = (captureOnlyInForeground ? RawInputDeviceFlags.NONE : RawInputDeviceFlags.INPUTSINK) | RawInputDeviceFlags.DEVNOTIFY; rid[0].Target = hwnd; if(!Win32.RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0]))) { throw new ApplicationException("Failed to register raw input device(s)."); } }
public RawInputDeviceEnumerator() { uint deviceCount = 0; var deviceSize = (uint)Marshal.SizeOf(typeof(Win32.RawInputDeviceList)); // first call retrieves the number of raw input devices var result = Win32.GetRawInputDeviceList( IntPtr.Zero, ref deviceCount, deviceSize); _devices = new RawInputDevice[deviceCount]; if ((int)result == -1 || deviceCount == 0) { // call failed, or no devices found return; } // allocates memory for an array of Win32.RawInputDeviceList IntPtr ptrDeviceList = Marshal.AllocHGlobal((int)(deviceSize * deviceCount)); result = Win32.GetRawInputDeviceList( ptrDeviceList, ref deviceCount, deviceSize); if ((int)result != -1) { // enumerates array of Win32.RawInputDeviceList, // and populates array of managed RawInputDevice objects for (var index = 0; index < deviceCount; index++) { var rawInputDeviceList = (Win32.RawInputDeviceList)Marshal.PtrToStructure( new IntPtr((ptrDeviceList.ToInt32() + (deviceSize * index))), typeof(Win32.RawInputDeviceList)); _devices[index] = new RawInputDevice(rawInputDeviceList); } } Marshal.FreeHGlobal(ptrDeviceList); }
public RawInputDriver(IntPtr hwnd) { RawInputDevice[] array = new RawInputDevice[2]; array[0].UsagePage = HidUsagePage.GENERIC; array[0].Usage = HidUsage.Keyboard; array[0].Flags = RawInputDeviceFlags.INPUTSINK; array[0].Target = hwnd; array[1].UsagePage = HidUsagePage.GENERIC; array[1].Usage = HidUsage.Mouse; array[1].Flags = RawInputDeviceFlags.INPUTSINK; array[1].Target = hwnd; if (!Win32.RegisterRawInputDevices(array, (uint)array.Length, (uint)Marshal.SizeOf(array[0]))) { DebugCenter.GetInstance().appendToFile("Failed to register raw input device(s)."); return; } this._parentWnd = hwnd; Win32.GetWindowThreadProcessId(this._parentWnd, out this._parentProcessID); }
internal static extern bool RegisterRawInputDevices(RawInputDevice[] pRawInputDevice, uint numberDevices, uint size);