Consolidating for keyboard and mouse device support.
authorStephaneLenclud
Wed, 04 Mar 2015 20:52:24 +0100
changeset 70e0a7b35f90dd
parent 69 b27d6b8527e2
child 71 c28dd93b94c5
Consolidating for keyboard and mouse device support.
Adding tests tab.
HidEvent.cs
MainForm.Designer.cs
RawInput.cs
RemoteControlDevice.cs
     1.1 --- a/HidEvent.cs	Tue Mar 03 21:07:44 2015 +0100
     1.2 +++ b/HidEvent.cs	Wed Mar 04 20:52:24 2015 +0100
     1.3 @@ -108,12 +108,17 @@
     1.4                  RAWINPUT rawInput = new RAWINPUT();
     1.5                  if (!Win32.RawInput.GetRawInputData(aMessage.LParam, ref rawInput, ref rawInputBuffer))
     1.6                  {
     1.7 +                    Debug.WriteLine("GetRawInputData failed!");
     1.8                      return;
     1.9                  }
    1.10  
    1.11 -
    1.12 -                //Get various information about this HID device
    1.13 -                Device = new Hid.HidDevice(rawInput.header.hDevice);
    1.14 +                //Our device can actually be null.
    1.15 +                //This is notably happening for some keyboard events
    1.16 +                if (rawInput.header.hDevice != IntPtr.Zero)
    1.17 +                {
    1.18 +                    //Get various information about this HID device
    1.19 +                    Device = new Hid.HidDevice(rawInput.header.hDevice);
    1.20 +                }
    1.21  
    1.22                  if (rawInput.header.dwType == Win32.RawInputDeviceType.RIM_TYPEHID)  //Check that our raw input is HID                        
    1.23                  {
    1.24 @@ -204,12 +209,15 @@
    1.25  
    1.26                      Debug.WriteLine("WM_INPUT source device is Keyboard.");
    1.27                      // do keyboard handling...
    1.28 -                    Debug.WriteLine("Type: " + Device.Info.keyboard.dwType.ToString());
    1.29 -                    Debug.WriteLine("SubType: " + Device.Info.keyboard.dwSubType.ToString());
    1.30 -                    Debug.WriteLine("Mode: " + Device.Info.keyboard.dwKeyboardMode.ToString());
    1.31 -                    Debug.WriteLine("Number of function keys: " + Device.Info.keyboard.dwNumberOfFunctionKeys.ToString());
    1.32 -                    Debug.WriteLine("Number of indicators: " + Device.Info.keyboard.dwNumberOfIndicators.ToString());
    1.33 -                    Debug.WriteLine("Number of keys total: " + Device.Info.keyboard.dwNumberOfKeysTotal.ToString());
    1.34 +                    if (Device != null)
    1.35 +                    {
    1.36 +                        Debug.WriteLine("Type: " + Device.Info.keyboard.dwType.ToString());
    1.37 +                        Debug.WriteLine("SubType: " + Device.Info.keyboard.dwSubType.ToString());
    1.38 +                        Debug.WriteLine("Mode: " + Device.Info.keyboard.dwKeyboardMode.ToString());
    1.39 +                        Debug.WriteLine("Number of function keys: " + Device.Info.keyboard.dwNumberOfFunctionKeys.ToString());
    1.40 +                        Debug.WriteLine("Number of indicators: " + Device.Info.keyboard.dwNumberOfIndicators.ToString());
    1.41 +                        Debug.WriteLine("Number of keys total: " + Device.Info.keyboard.dwNumberOfKeysTotal.ToString());
    1.42 +                    }
    1.43                  }
    1.44              }
    1.45              finally
    1.46 @@ -272,7 +280,12 @@
    1.47                  Debug.WriteLine("==== Invalid HidEvent");
    1.48                  return;
    1.49              }
    1.50 -            Device.DebugWrite();
    1.51 +
    1.52 +            if (Device!=null)
    1.53 +            {
    1.54 +                Device.DebugWrite();
    1.55 +            }
    1.56 +            
    1.57              if (IsGeneric) Debug.WriteLine("==== Generic");
    1.58              if (IsKeyboard) Debug.WriteLine("==== Keyboard");
    1.59              if (IsMouse) Debug.WriteLine("==== Mouse");
    1.60 @@ -292,6 +305,11 @@
    1.61          /// <returns></returns>
    1.62          public string InputReportString()
    1.63          {
    1.64 +            if (InputReport == null)
    1.65 +            {
    1.66 +                return "null";
    1.67 +            }
    1.68 +
    1.69              string hidDump = "";
    1.70              foreach (byte b in InputReport)
    1.71              {
     2.1 --- a/MainForm.Designer.cs	Tue Mar 03 21:07:44 2015 +0100
     2.2 +++ b/MainForm.Designer.cs	Wed Mar 04 20:52:24 2015 +0100
     2.3 @@ -26,15 +26,18 @@
     2.4              this.columnHeaderRepeat = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
     2.5              this.columnHeaderTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
     2.6              this.tabPageDevices = new System.Windows.Forms.TabPage();
     2.7 +            this.buttonTreeViewExpandAll = new System.Windows.Forms.Button();
     2.8 +            this.buttonTreeViewCollapseAll = new System.Windows.Forms.Button();
     2.9              this.treeViewDevices = new System.Windows.Forms.TreeView();
    2.10 -            this.buttonTreeViewCollapseAll = new System.Windows.Forms.Button();
    2.11 -            this.buttonTreeViewExpandAll = new System.Windows.Forms.Button();
    2.12              this.statusStrip = new System.Windows.Forms.StatusStrip();
    2.13              this.toolStripStatusLabelDevice = new System.Windows.Forms.ToolStripStatusLabel();
    2.14 +            this.tabPageTests = new System.Windows.Forms.TabPage();
    2.15 +            this.textBoxTests = new System.Windows.Forms.TextBox();
    2.16              this.tabControl.SuspendLayout();
    2.17              this.tabPageMessages.SuspendLayout();
    2.18              this.tabPageDevices.SuspendLayout();
    2.19              this.statusStrip.SuspendLayout();
    2.20 +            this.tabPageTests.SuspendLayout();
    2.21              this.SuspendLayout();
    2.22              // 
    2.23              // labelButtonName
    2.24 @@ -73,6 +76,7 @@
    2.25              | System.Windows.Forms.AnchorStyles.Left)));
    2.26              this.tabControl.Controls.Add(this.tabPageMessages);
    2.27              this.tabControl.Controls.Add(this.tabPageDevices);
    2.28 +            this.tabControl.Controls.Add(this.tabPageTests);
    2.29              this.tabControl.Location = new System.Drawing.Point(12, 12);
    2.30              this.tabControl.Name = "tabControl";
    2.31              this.tabControl.SelectedIndex = 0;
    2.32 @@ -88,7 +92,7 @@
    2.33              this.tabPageMessages.Location = new System.Drawing.Point(4, 22);
    2.34              this.tabPageMessages.Name = "tabPageMessages";
    2.35              this.tabPageMessages.Padding = new System.Windows.Forms.Padding(3);
    2.36 -            this.tabPageMessages.Size = new System.Drawing.Size(894, 488);
    2.37 +            this.tabPageMessages.Size = new System.Drawing.Size(894, 506);
    2.38              this.tabPageMessages.TabIndex = 0;
    2.39              this.tabPageMessages.Text = "Messages";
    2.40              this.tabPageMessages.UseVisualStyleBackColor = true;
    2.41 @@ -109,7 +113,7 @@
    2.42              this.listViewEvents.GridLines = true;
    2.43              this.listViewEvents.Location = new System.Drawing.Point(8, 6);
    2.44              this.listViewEvents.Name = "listViewEvents";
    2.45 -            this.listViewEvents.Size = new System.Drawing.Size(744, 474);
    2.46 +            this.listViewEvents.Size = new System.Drawing.Size(744, 492);
    2.47              this.listViewEvents.TabIndex = 3;
    2.48              this.listViewEvents.UseCompatibleStateImageBehavior = false;
    2.49              this.listViewEvents.View = System.Windows.Forms.View.Details;
    2.50 @@ -157,14 +161,15 @@
    2.51              this.tabPageDevices.Text = "Devices";
    2.52              this.tabPageDevices.UseVisualStyleBackColor = true;
    2.53              // 
    2.54 -            // treeViewDevices
    2.55 +            // buttonTreeViewExpandAll
    2.56              // 
    2.57 -            this.treeViewDevices.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    2.58 -            | System.Windows.Forms.AnchorStyles.Left)));
    2.59 -            this.treeViewDevices.Location = new System.Drawing.Point(8, 6);
    2.60 -            this.treeViewDevices.Name = "treeViewDevices";
    2.61 -            this.treeViewDevices.Size = new System.Drawing.Size(713, 492);
    2.62 -            this.treeViewDevices.TabIndex = 0;
    2.63 +            this.buttonTreeViewExpandAll.Location = new System.Drawing.Point(813, 6);
    2.64 +            this.buttonTreeViewExpandAll.Name = "buttonTreeViewExpandAll";
    2.65 +            this.buttonTreeViewExpandAll.Size = new System.Drawing.Size(75, 23);
    2.66 +            this.buttonTreeViewExpandAll.TabIndex = 2;
    2.67 +            this.buttonTreeViewExpandAll.Text = "Expand All";
    2.68 +            this.buttonTreeViewExpandAll.UseVisualStyleBackColor = true;
    2.69 +            this.buttonTreeViewExpandAll.Click += new System.EventHandler(this.buttonTreeViewExpandAll_Click);
    2.70              // 
    2.71              // buttonTreeViewCollapseAll
    2.72              // 
    2.73 @@ -176,15 +181,14 @@
    2.74              this.buttonTreeViewCollapseAll.UseVisualStyleBackColor = true;
    2.75              this.buttonTreeViewCollapseAll.Click += new System.EventHandler(this.buttonTreeViewCollapseAll_Click);
    2.76              // 
    2.77 -            // buttonTreeViewExpandAll
    2.78 +            // treeViewDevices
    2.79              // 
    2.80 -            this.buttonTreeViewExpandAll.Location = new System.Drawing.Point(813, 6);
    2.81 -            this.buttonTreeViewExpandAll.Name = "buttonTreeViewExpandAll";
    2.82 -            this.buttonTreeViewExpandAll.Size = new System.Drawing.Size(75, 23);
    2.83 -            this.buttonTreeViewExpandAll.TabIndex = 2;
    2.84 -            this.buttonTreeViewExpandAll.Text = "Expand All";
    2.85 -            this.buttonTreeViewExpandAll.UseVisualStyleBackColor = true;
    2.86 -            this.buttonTreeViewExpandAll.Click += new System.EventHandler(this.buttonTreeViewExpandAll_Click);
    2.87 +            this.treeViewDevices.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    2.88 +            | System.Windows.Forms.AnchorStyles.Left)));
    2.89 +            this.treeViewDevices.Location = new System.Drawing.Point(8, 6);
    2.90 +            this.treeViewDevices.Name = "treeViewDevices";
    2.91 +            this.treeViewDevices.Size = new System.Drawing.Size(713, 492);
    2.92 +            this.treeViewDevices.TabIndex = 0;
    2.93              // 
    2.94              // statusStrip
    2.95              // 
    2.96 @@ -202,6 +206,24 @@
    2.97              this.toolStripStatusLabelDevice.Size = new System.Drawing.Size(61, 17);
    2.98              this.toolStripStatusLabelDevice.Text = "No Device";
    2.99              // 
   2.100 +            // tabPageTests
   2.101 +            // 
   2.102 +            this.tabPageTests.Controls.Add(this.textBoxTests);
   2.103 +            this.tabPageTests.Location = new System.Drawing.Point(4, 22);
   2.104 +            this.tabPageTests.Name = "tabPageTests";
   2.105 +            this.tabPageTests.Size = new System.Drawing.Size(894, 506);
   2.106 +            this.tabPageTests.TabIndex = 2;
   2.107 +            this.tabPageTests.Text = "Tests";
   2.108 +            this.tabPageTests.UseVisualStyleBackColor = true;
   2.109 +            // 
   2.110 +            // textBoxTests
   2.111 +            // 
   2.112 +            this.textBoxTests.Location = new System.Drawing.Point(4, 4);
   2.113 +            this.textBoxTests.Multiline = true;
   2.114 +            this.textBoxTests.Name = "textBoxTests";
   2.115 +            this.textBoxTests.Size = new System.Drawing.Size(887, 499);
   2.116 +            this.textBoxTests.TabIndex = 0;
   2.117 +            // 
   2.118              // MainForm
   2.119              // 
   2.120              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   2.121 @@ -218,6 +240,8 @@
   2.122              this.tabPageDevices.ResumeLayout(false);
   2.123              this.statusStrip.ResumeLayout(false);
   2.124              this.statusStrip.PerformLayout();
   2.125 +            this.tabPageTests.ResumeLayout(false);
   2.126 +            this.tabPageTests.PerformLayout();
   2.127              this.ResumeLayout(false);
   2.128              this.PerformLayout();
   2.129  
   2.130 @@ -228,6 +252,8 @@
   2.131          private System.Windows.Forms.Button buttonTreeViewCollapseAll;
   2.132          private System.Windows.Forms.StatusStrip statusStrip;
   2.133          private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelDevice;
   2.134 +        private System.Windows.Forms.TabPage tabPageTests;
   2.135 +        private System.Windows.Forms.TextBox textBoxTests;
   2.136  
   2.137      }
   2.138  }
     3.1 --- a/RawInput.cs	Tue Mar 03 21:07:44 2015 +0100
     3.2 +++ b/RawInput.cs	Wed Mar 04 20:52:24 2015 +0100
     3.3 @@ -151,7 +151,7 @@
     3.4  
     3.5  
     3.6          /// <summary>
     3.7 -        /// 
     3.8 +        /// Populate the given tree-view control with our Raw Input Devices.
     3.9          /// </summary>
    3.10          /// <param name="aTreeView"></param>
    3.11          public static void PopulateDeviceList(TreeView aTreeView)
     4.1 --- a/RemoteControlDevice.cs	Tue Mar 03 21:07:44 2015 +0100
     4.2 +++ b/RemoteControlDevice.cs	Wed Mar 04 20:52:24 2015 +0100
     4.3 @@ -179,7 +179,7 @@
     4.4              // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
     4.5              // for the vendor defined usage page.
     4.6  
     4.7 -            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[4];
     4.8 +            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[6];
     4.9  
    4.10              int i = 0;
    4.11              rid[i].usUsagePage = (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl;
    4.12 @@ -205,21 +205,21 @@
    4.13              rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    4.14              rid[i].hwndTarget = aHWND;
    4.15  
    4.16 -			//i++;
    4.17 +			i++;
    4.18  			rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
    4.19  			rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.GamePad;
    4.20  			rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    4.21  			rid[i].hwndTarget = aHWND;
    4.22  
    4.23 -            //i++;
    4.24 -            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
    4.25 -            //rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.Keyboard;
    4.26 +            i++;
    4.27 +            rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
    4.28 +            rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Keyboard;
    4.29              //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    4.30 -            //rid[i].hwndTarget = aHWND;
    4.31 +            rid[i].hwndTarget = aHWND;
    4.32  
    4.33              //i++;
    4.34              //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
    4.35 -            //rid[i].usUsage = (ushort)Hid.UsageCollectionGenericDesktop.Mouse;
    4.36 +            //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
    4.37              //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    4.38              //rid[i].hwndTarget = aHWND;
    4.39