title: How to distinguish between multiple input devices in C#
body: <p>I have a barcode scanner (which acts like a keyboard) and of course I have a keyboard too hooked up to a computer. The software is accepting input from both the scanner and the keyboard. I need to accept only the scanner's input. The code is written in C#. Is there a way to "disable" input from the keyboard and only accept input from the scanner?</p>
  
  <p>Note:
  Keyboard is part of a laptop...so it cannot be unplugged. Also, I tried putting the following code 
      protected override Boolean ProcessDialogKey(System.Windows.Forms.Keys keyData)
      {
        return true;
      }
  But then along with ignoring the keystrokes from the keyboard, the barcode scanner input is also ignored.</p>
  
  <p>I cannot have the scanner send sentinal characters as, the scanner is being used by other applications and adding a sentinal character stream would mean modifying other code.</p>
  
  <p>Also, I cannot use the timing method of determining if the input came from a barcode scanner (if its a bunch of characters followed by a pause) since the barcodes scanned could potentially be single character barcodes.</p>
  
  <p>Yes, I am reading data from a stream.</p>
  
  <p>I am trying to follow along with the article: Distinguishing Barcode Scanners from the Keyboard in WinForms. However I have the following questions:</p>
  
  <ol>
  <li>I get an error NativeMethods is inaccessible due to its protection level. It seems as though I need to import a dll; is this correct? If so, how do I do it? </li>
  <li>Which protected override void WndProc(ref Message m) definition should I use, there are two implementations in the article?</li>
  <li>Am getting an error related to [SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] error CS0246: The type or namespace name 'SecurityPermission' could not be found (are you missing a using directive or an assembly reference?). How do I resolve this error?</li>
  <li>There is also an error on the line containing: if ((from hardwareId in hardwareIds where deviceName.Contains(hardwareId) select hardwareId).Count() > 0) Error is error CS1026: ) expected.</li>
  <li>Should I be placing all the code in the article in one .cs file called BarcodeScannerListener.cs?</li>
  </ol>
  
  <p>Followup questions about C# solution source code posted by Nicholas Piasecki on <a href="http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/" rel="noreferrer">http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/</a>:</p>
  
  <ol>
  <li>I was not able to open the solution in VS 2005, so I downloaded Visual C# 2008 Express Edition, and the code ran. However, after hooking up my barcode scanner and scanning a barcode, the program did not recognize the scan. I put a break point in OnBarcodeScanned method but it never got hit. I did change the App.config with the id of my Barcode scanner obtained using Device Manager. There seems to be 2 deviceNames with HID#Vid_0536&amp;Pid_01c1 (which is obtained from Device Manager when the scanner is hooked up). I don't know if this is causing the scanning not to work. When iterating over the deviceNames, here is the list of devices I found (using the debugger):</li>
  </ol>
  
  <p>"\??\HID#Vid_0536&amp;Pid_01c1&amp;MI_01#9&amp;25ca5370&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"</p>
  
  <p>"\??\HID#Vid_0536&amp;Pid_01c1&amp;MI_00#9&amp;38e10b9&amp;0&amp;0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"</p>
  
  <p>"\??\HID#Vid_413c&amp;Pid_2101&amp;MI_00#8&amp;1966e83d&amp;0&amp;0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"</p>
  
  <p>"\??\HID#Vid_413c&amp;Pid_3012#7&amp;960fae0&amp;0&amp;0000#{378de44c-56ef-11d1-bc8c-00a0c91405dd}"<br>
  "\??\Root#RDP_KBD#0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"
  "\??\ACPI#PNP0303#4&amp;2f94427b&amp;0#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"
  "\??\Root#RDP_MOU#0000#{378de44c-56ef-11d1-bc8c-00a0c91405dd}"
  "\??\ACPI#PNP0F13#4&amp;2f94427b&amp;0#{378de44c-56ef-11d1-bc8c-00a0c91405dd}"</p>
  
  <p>So there are 2 entries for HID#Vid_0536&amp;Pid_01c1; could that be causing the scanning not to work?</p>
  
  <p>OK so it seems that I had to figure out a way to not depend on the ASCII 0x04 character being sent by the scanner...since my scanner does not send that character. After that, the barcode scanned event is fired and the popup with the barcode is shown. So thanks Nicholas for your help. </p>