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; }
internal RawInputEventArgs(RawKeyboardDevice device, KeyPressState keyPressState, uint message, Key key, int virtualKey) { Device = device; KeyPressState = keyPressState; Message = message; Key = key; VirtualKey = virtualKey; }
public void EnumerateDevices() { lock (_lock) { _deviceList.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."); _deviceList.Add(rawKeyboardDevice.Handle, rawKeyboardDevice); var devices = 0u; var size = Marshal.SizeOf(typeof(RawInputDeviceList)); if (Win32Methods.GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)size) != 0u) { throw new Win32Exception(Marshal.GetLastWin32Error()); } var pRawInputDeviceList = Marshal.AllocHGlobal((int)(size * devices)); try { Win32Methods.GetRawInputDeviceList(pRawInputDeviceList, ref devices, (uint)size); var index = 0; while (index < devices) { var device = GetDevice(pRawInputDeviceList, size, index); if (device != null && !_deviceList.ContainsKey(device.Handle)) { _deviceList.Add(device.Handle, device); } index++; } } finally { Marshal.FreeHGlobal(pRawInputDeviceList); } NumberOfKeyboards = _deviceList.Count; } }
public void EnumerateDevices() { lock (_lock) { _deviceList.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."); _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); } NumberOfKeyboards = _deviceList.Count; } }