public static IDictionary<IntPtr, RawKeyboardDevice> GetDevices() { var deviceList = new Dictionary<IntPtr, RawKeyboardDevice>(); var rawKeyboardDevice = new RawKeyboardDevice("Global Keyboard", RawDeviceType.Keyboard, IntPtr.Zero, "Fake Keyboard. Some keys (ZOOM, MUTE, VOLUMEUP, VOLUMEDOWN) are sent to rawinput with a handle of zero."); deviceList.Add(rawKeyboardDevice.Handle, rawKeyboardDevice); uint devices = 0u; int size = Marshal.SizeOf(typeof(RawInputDeviceList)); if (Win32Methods.GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)size) != 0u) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(size * devices)); try { Win32Methods.GetRawInputDeviceList(pRawInputDeviceList, ref devices, (uint)size); int index = 0; while (index < devices) { RawKeyboardDevice device = GetDevice(pRawInputDeviceList, size, index); if (device != null && !deviceList.ContainsKey(device.Handle)) { deviceList.Add(device.Handle, device); } index++; } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } return deviceList; }
public void EnumerateMice() { lock (_lock) { _mouseList.Clear(); uint devices = 0u; int size = Marshal.SizeOf(typeof(RawInputDeviceList)); if (Win32Methods.GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)size) != 0u) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(size * devices)); try { Win32Methods.GetRawInputDeviceList(pRawInputDeviceList, ref devices, (uint)size); int index = 0; while (index < devices) { RawMouseDevice device = GetMouse(pRawInputDeviceList, size, index); if (device != null && !_mouseList.ContainsKey(device.Handle)) { _mouseList.Add(device.Handle, device); } index++; } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } NumberOfMice = _mouseList.Count; } }
public void EnumerateHid() { lock (_lock) { _hidList.Clear(); uint devices = 0u; int size = Marshal.SizeOf(typeof(RawInputDeviceList)); if (Win32Methods.GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)size) != 0u) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(size * devices)); try { Win32Methods.GetRawInputDeviceList(pRawInputDeviceList, ref devices, (uint)size); int index = 0; while (index < devices) { RawHidDevice device = GetHid(pRawInputDeviceList, size, index); if (device != null && !_hidList.ContainsKey(device.Handle)) { Debug.WriteLine("Added Hid; Handle: {0}, Name: {1}, Type: {2}, Description: {3}", (uint)device.Handle, device.Name, device.Type, device.Description); _hidList.Add(device.Handle, device); } index++; } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } NumberOfHid = _hidList.Count; } }
public void EnumerateKeyboards() { lock (_lock) { _keyboardList.Clear(); var rawKeyboardDevice = new RawKeyboardDevice("Global Keyboard", RawDeviceType.Keyboard, IntPtr.Zero, "Fake Keyboard. Some keys (ZOOM, MUTE, VOLUMEUP, VOLUMEDOWN) are sent to rawinput with a handle of zero."); _keyboardList.Add(rawKeyboardDevice.Handle, rawKeyboardDevice); uint devices = 0u; int size = Marshal.SizeOf(typeof(RawInputDeviceList)); if (Win32Methods.GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)size) != 0u) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(size * devices)); try { Win32Methods.GetRawInputDeviceList(pRawInputDeviceList, ref devices, (uint)size); int index = 0; while (index < devices) { RawKeyboardDevice device = GetKeyboard(pRawInputDeviceList, size, index); if (device != null && !_keyboardList.ContainsKey(device.Handle)) { _keyboardList.Add(device.Handle, device); } index++; } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } NumberOfKeyboards = _keyboardList.Count; } }