示例#1
0
        private static string GetScannerId(RawKeyboardDevice device)
        {
            var regex = new Regex(@"(?<={)(.*)(?=})");

            if (regex.IsMatch(device.Name))
            {
                return(regex.Match(device.Name).Value);
            }
            return(device.Name);
        }
示例#2
0
        private void OnKeyPressed(object sender, RawKeyEventArgs e)
        {
            Event = e;
            if (_keyboardDevices.TryGetValue(e.Device, out RawKeyboardDevice dev))
            {
                Device = dev;
            }
            if (e.Device == new IntPtr(0x1b21034f) && ShouldHandle.IsChecked == true)
            {
                e.Handled = true;
            }

            DeviceCount = _keyboardDevices.Count;
        }
示例#3
0
        protected override void OnSourceInitialized(EventArgs e)
        {
            if (!(PresentationSource.FromVisual(this) is HwndSource source))
            {
                throw new InvalidOperationException("Cannot find a valid HwndSource");
            }

            _keyboardHwndSourceHook                    = new KeyboardHwndSourceHook(source, true);
            _keyboardHwndSourceHook.KeyPressed        += OnKeyPressed;
            _keyboardHwndSourceHook.InputDeviceChange += OnInputDeviceChange;

            _keyboardDevices = RawKeyboardDevice.GetDevices();

            DeviceCount = _keyboardDevices.Count;

            base.OnSourceInitialized(e);
        }
        public static string GetDeviceDiagnostics()
        {
            var  stringBuilder = new StringBuilder();
            uint devices       = 0u;
            int  listSize      = Marshal.SizeOf(typeof(RawInputDeviceList));

            if (GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)listSize) != 0u)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            var deviceListPtr = Marshal.AllocHGlobal((int)(listSize * devices));

            try
            {
                GetRawInputDeviceList(deviceListPtr, ref devices, (uint)listSize);
                int index = 0;
                while (index < devices)
                {
                    uint pcbSize            = 0u;
                    var  rawInputDeviceList = (RawInputDeviceList)Marshal.PtrToStructure(new IntPtr(deviceListPtr.ToInt64() + listSize * index), typeof(RawInputDeviceList));
                    GetRawInputDeviceInfo(rawInputDeviceList.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);
                    if (pcbSize <= 0u)
                    {
                        stringBuilder.AppendLine("pcbSize: " + pcbSize);
                        stringBuilder.AppendLine(Marshal.GetLastWin32Error().ToString());
                        string result = stringBuilder.ToString();
                        return(result);
                    }
                    var deviceInfoSize = (uint)Marshal.SizeOf(typeof(DeviceInfo));
                    var deviceInfo     = new DeviceInfo
                    {
                        Size = Marshal.SizeOf(typeof(DeviceInfo))
                    };
                    if (GetRawInputDeviceInfo(rawInputDeviceList.hDevice, 536870923u, ref deviceInfo, ref deviceInfoSize) <= 0u)
                    {
                        stringBuilder.AppendLine(Marshal.GetLastWin32Error().ToString());
                        string result = stringBuilder.ToString();
                        return(result);
                    }
                    var deviceInfoPtr = Marshal.AllocHGlobal((int)pcbSize);
                    try
                    {
                        GetRawInputDeviceInfo(rawInputDeviceList.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, deviceInfoPtr,
                                              ref pcbSize);
                        string device = Marshal.PtrToStringAnsi(deviceInfoPtr);
                        if (rawInputDeviceList.dwType == DeviceType.RimTypekeyboard ||
                            rawInputDeviceList.dwType == DeviceType.RimTypeHid)
                        {
                            string deviceDescription = GetDeviceDescription(device);
                            var    rawKeyboardDevice = new RawKeyboardDevice(Marshal.PtrToStringAnsi(deviceInfoPtr),
                                                                             (RawDeviceType)rawInputDeviceList.dwType, rawInputDeviceList.hDevice, deviceDescription);
                            stringBuilder.AppendLine(rawKeyboardDevice.ToString());
                            stringBuilder.AppendLine(deviceInfo.ToString());
                            stringBuilder.AppendLine(deviceInfo.KeyboardInfo.ToString());
                            stringBuilder.AppendLine(deviceInfo.HIDInfo.ToString());
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(deviceInfoPtr);
                    }
                    index++;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(deviceListPtr);
            }
            return(stringBuilder.ToString());
        }
示例#5
0
 private void OnInputDeviceChange(object sender, EventArgs e)
 {
     _keyboardDevices = RawKeyboardDevice.GetDevices();
 }
 public static string GetDeviceDiagnostics()
 {
     var stringBuilder = new StringBuilder();
     uint devices = 0u;
     int listSize = Marshal.SizeOf(typeof(RawInputDeviceList));
     if (GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)listSize) != 0u)
     {
         throw new Win32Exception(Marshal.GetLastWin32Error());
     }
     var deviceListPtr = Marshal.AllocHGlobal((int)(listSize * devices));
     try
     {
         GetRawInputDeviceList(deviceListPtr, ref devices, (uint)listSize);
         int index = 0;
         while (index < devices)
         {
             uint pcbSize = 0u;
             var rawInputDeviceList = (RawInputDeviceList)Marshal.PtrToStructure(new IntPtr(deviceListPtr.ToInt64() + listSize * index), typeof(RawInputDeviceList));
             GetRawInputDeviceInfo(rawInputDeviceList.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);
             if (pcbSize <= 0u)
             {
                 stringBuilder.AppendLine("pcbSize: " + pcbSize);
                 stringBuilder.AppendLine(Marshal.GetLastWin32Error().ToString());
                 string result = stringBuilder.ToString();
                 return result;
             }
             var deviceInfoSize = (uint)Marshal.SizeOf(typeof(DeviceInfo));
             var deviceInfo = new DeviceInfo
             {
                 Size = Marshal.SizeOf(typeof(DeviceInfo))
             };
             if (GetRawInputDeviceInfo(rawInputDeviceList.hDevice, 536870923u, ref deviceInfo, ref deviceInfoSize) <= 0u)
             {
                 stringBuilder.AppendLine(Marshal.GetLastWin32Error().ToString());
                 string result = stringBuilder.ToString();
                 return result;
             }
             var deviceInfoPtr = Marshal.AllocHGlobal((int)pcbSize);
             try
             {
                 GetRawInputDeviceInfo(rawInputDeviceList.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, deviceInfoPtr,
                     ref pcbSize);
                 string device = Marshal.PtrToStringAnsi(deviceInfoPtr);
                 if (rawInputDeviceList.dwType == DeviceType.RimTypekeyboard ||
                     rawInputDeviceList.dwType == DeviceType.RimTypeHid)
                 {
                     string deviceDescription = GetDeviceDescription(device);
                     var rawKeyboardDevice = new RawKeyboardDevice(Marshal.PtrToStringAnsi(deviceInfoPtr),
                         (RawDeviceType)rawInputDeviceList.dwType, rawInputDeviceList.hDevice, deviceDescription);
                     stringBuilder.AppendLine(rawKeyboardDevice.ToString());
                     stringBuilder.AppendLine(deviceInfo.ToString());
                     stringBuilder.AppendLine(deviceInfo.KeyboardInfo.ToString());
                     stringBuilder.AppendLine(deviceInfo.HIDInfo.ToString());
                 }
             }
             finally
             {
                 Marshal.FreeHGlobal(deviceInfoPtr);
             }
             index++;
         }
     }
     finally
     {
         Marshal.FreeHGlobal(deviceListPtr);
     }
     return stringBuilder.ToString();
 }