Adding method to get the device name.
authorsl
Sat, 06 Dec 2014 02:46:46 +0100
changeset 228b2a3da54187
parent 21 fb4f80d5cf57
child 23 743cadfacda0
Adding method to get the device name.
RawInput.cs
RemoteControlDevice.cs
Win32RawInput.cs
     1.1 --- a/RawInput.cs	Sat Dec 06 01:53:55 2014 +0100
     1.2 +++ b/RawInput.cs	Sat Dec 06 02:46:46 2014 +0100
     1.3 @@ -68,7 +68,7 @@
     1.4                  uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
     1.5                  deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
     1.6  
     1.7 -                int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
     1.8 +                int res = Win32.Function.GetRawInputDeviceInfoW(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
     1.9                  if (res <= 0)
    1.10                  {
    1.11                      Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
    1.12 @@ -101,7 +101,7 @@
    1.13          public static IntPtr GetPreParsedData(IntPtr hDevice)
    1.14          {
    1.15              uint ppDataSize = 256;
    1.16 -            int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
    1.17 +            int result = Win32.Function.GetRawInputDeviceInfoW(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
    1.18              if (result != 0)
    1.19              {
    1.20                  Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error());
    1.21 @@ -109,7 +109,7 @@
    1.22              }
    1.23  
    1.24              IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
    1.25 -            result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
    1.26 +            result = Win32.Function.GetRawInputDeviceInfoW(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
    1.27              if (result <= 0)
    1.28              {
    1.29                  Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error());
    1.30 @@ -118,6 +118,37 @@
    1.31              return ppData;
    1.32          }
    1.33  
    1.34 +        /// <summary>
    1.35 +        /// 
    1.36 +        /// </summary>
    1.37 +        /// <param name="device"></param>
    1.38 +        /// <returns></returns>
    1.39 +        public static string GetDeviceName(IntPtr device)
    1.40 +        {
    1.41 +            uint deviceNameSize = 256;
    1.42 +            int result = Win32.Function.GetRawInputDeviceInfoW(device, Win32.Const.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize);
    1.43 +            if (result != 0)
    1.44 +            {
    1.45 +                return string.Empty;
    1.46 +            }
    1.47 +
    1.48 +            IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2);  // size is the character count not byte count
    1.49 +            try
    1.50 +            {
    1.51 +                result = Win32.Function.GetRawInputDeviceInfoW(device, Win32.Const.RIDI_DEVICENAME, deviceName, ref deviceNameSize);
    1.52 +                if (result > 0)
    1.53 +                {
    1.54 +                    return Marshal.PtrToStringUni(deviceName, result - 1); // -1 for NULL termination
    1.55 +                }
    1.56 +
    1.57 +                return string.Empty;
    1.58 +            }
    1.59 +            finally
    1.60 +            {
    1.61 +                Marshal.FreeHGlobal(deviceName);
    1.62 +            }
    1.63 +        }
    1.64 +
    1.65  
    1.66      }
    1.67  }
    1.68 \ No newline at end of file
     2.1 --- a/RemoteControlDevice.cs	Sat Dec 06 01:53:55 2014 +0100
     2.2 +++ b/RemoteControlDevice.cs	Sat Dec 06 02:46:46 2014 +0100
     2.3 @@ -172,7 +172,7 @@
     2.4  			// remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
     2.5  			// for the vendor defined usage page.
     2.6  
     2.7 -			RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[3];
     2.8 +			RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
     2.9  
    2.10              int i = 0;
    2.11  			rid[i].usUsagePage = (ushort)Hid.UsagePage.MceRemote;
    2.12 @@ -192,17 +192,17 @@
    2.13              rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    2.14              rid[i].hwndTarget = aHWND;
    2.15  
    2.16 -            //i++;
    2.17 -            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
    2.18 -            //rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.SystemControl;
    2.19 -            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    2.20 -            //rid[i].hwndTarget = aHWND;
    2.21 +            i++;
    2.22 +            rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
    2.23 +            rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.SystemControl;
    2.24 +            rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    2.25 +            rid[i].hwndTarget = aHWND;
    2.26  
    2.27 -            //i++;
    2.28 -            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
    2.29 -            //rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.Keyboard;
    2.30 -            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    2.31 -            //rid[i].hwndTarget = aHWND;
    2.32 +            i++;
    2.33 +            rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
    2.34 +            rid[i].usUsage = (ushort)Hid.UsageIdGenericDesktop.Keyboard;
    2.35 +            rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    2.36 +            rid[i].hwndTarget = aHWND;
    2.37  
    2.38              //i++;
    2.39              //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControl;
    2.40 @@ -251,13 +251,22 @@
    2.41  			{
    2.42  				case (int) Keys.Escape:
    2.43  					rcb = RemoteControlButton.Clear;
    2.44 -					break;
    2.45 +                    break;
    2.46 +                case (int)Keys.Up:
    2.47 +                    rcb = RemoteControlButton.Up;
    2.48 +                    break;
    2.49  				case (int) Keys.Down:
    2.50  					rcb = RemoteControlButton.Down;
    2.51  					break;
    2.52  				case (int) Keys.Left:
    2.53  					rcb = RemoteControlButton.Left;
    2.54 -					break;
    2.55 +                    break;
    2.56 +                case (int)Keys.Right:
    2.57 +                    rcb = RemoteControlButton.Right;
    2.58 +                    break;
    2.59 +                case (int)Keys.Enter:
    2.60 +                    rcb = RemoteControlButton.Enter;
    2.61 +                    break;
    2.62  				case (int) Keys.D0:
    2.63  					rcb = RemoteControlButton.Digit0;
    2.64  					break;
    2.65 @@ -287,20 +296,14 @@
    2.66  					break;
    2.67  				case (int) Keys.D9:
    2.68  					rcb = RemoteControlButton.Digit9;
    2.69 -					break;
    2.70 -				case (int) Keys.Enter:
    2.71 -					rcb = RemoteControlButton.Enter;
    2.72 -					break;
    2.73 -				case (int) Keys.Right:
    2.74 -					rcb = RemoteControlButton.Right;
    2.75 -					break;
    2.76 -				case (int) Keys.Up:
    2.77 -					rcb = RemoteControlButton.Up;
    2.78 -					break;
    2.79 +                    break;
    2.80  			}
    2.81  
    2.82 -			if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
    2.83 +            if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
    2.84 +            {
    2.85 +                Debug.WriteLine("KeyDown: " + rcb.ToString());
    2.86                  this.ButtonPressed(this, new RemoteControlEventArgs(rcb, InputDevice.Key));
    2.87 +            }
    2.88  		}
    2.89  
    2.90  
    2.91 @@ -395,6 +398,9 @@
    2.92                  {
    2.93                      return;
    2.94                  }
    2.95 +
    2.96 +                //
    2.97 +                Debug.WriteLine(RawInput.GetDeviceName(rawInput.header.hDevice));
    2.98                 
    2.99  
   2.100                  if (rawInput.header.dwType == Const.RIM_TYPEHID)  //Check that our raw input is HID                        
   2.101 @@ -488,7 +494,12 @@
   2.102                  {
   2.103                      Debug.WriteLine("WM_INPUT source device is Keyboard.");
   2.104                      // do keyboard handling...
   2.105 -
   2.106 +                    Debug.WriteLine("Type: " + deviceInfo.keyboard.dwType.ToString());
   2.107 +                    Debug.WriteLine("SubType: " + deviceInfo.keyboard.dwSubType.ToString());
   2.108 +                    Debug.WriteLine("Mode: " + deviceInfo.keyboard.dwKeyboardMode.ToString());
   2.109 +                    Debug.WriteLine("Number of function keys: " + deviceInfo.keyboard.dwNumberOfFunctionKeys.ToString());
   2.110 +                    Debug.WriteLine("Number of indicators: " + deviceInfo.keyboard.dwNumberOfIndicators.ToString());
   2.111 +                    Debug.WriteLine("Number of keys total: " + deviceInfo.keyboard.dwNumberOfKeysTotal.ToString());                   
   2.112                  }
   2.113              }
   2.114              finally
     3.1 --- a/Win32RawInput.cs	Sat Dec 06 01:53:55 2014 +0100
     3.2 +++ b/Win32RawInput.cs	Sat Dec 06 02:46:46 2014 +0100
     3.3 @@ -13,7 +13,7 @@
     3.4  		public extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
     3.5  
     3.6     		[DllImport("User32.dll", SetLastError=true)]
     3.7 -		public extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
     3.8 +		public extern static int GetRawInputDeviceInfoW(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
     3.9      }
    3.10  
    3.11