2

I have two devices, a Barcode-Scanner and a Keyboard, connect over USB to my PC. My goal is to read all the input from the Barcode-Scanner and process this with my program. Furthermore the input of the Scanner should be blocked for the OS.

I found a good article to this topic that is called: Combining Raw Input and keyboard Hook to selectively block input from multiple keyboards

With this approach I can get the input (and from which device this input comes) for my programm and block it for the OS, like I want to. But this approach is pretty complicated because it only works with a combination of inputEvents and Hooks. The Hooks are there for blocking the data for the OS. The problem is that there is no way to tell from which device the Hook comes.

My question is: Does anyone know an other way to block input from a special keyboard that does not work with this combination of inputEvents and Hooks? Or maybe there is a possibility to tell from which device a Hook comes?

Andre
  • 1,249
  • 1
  • 15
  • 38
  • It is possible to attach additional information to a window message (see [SetMessageExtraInfo](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644954.aspx)). Have you checked the documentation for the barcode scanner to see, if it maybe does provide additional information? – IInspectable Oct 30 '15 at 12:02

2 Answers2

-3

I use timer.tick create array for numpadkeys and loop through that

GetAsyncKeyState(VK_LBUTTON) 

otherwise https://msdn.microsoft.com/en-us/library/windows/desktop/ms646290(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms646298(v=vs.85).aspx

How to Check if User input is from Barcode Scanner or Keyboard?

It is relatively easy done with RAW Input API.

Take a look at "Distinguishing Barcode Scanners from the Keyboard in WinForms"

I have a program that reads 3 different USB scanners and redirects the input to 3 different "channels" for processing. The code is somewhat extensive, so I am not postin it here. If you wish, I can paste some chunks of it or send you the project in e-mail.

As a clue are the imports:

#region Raw Input API

[DllImport( "User32.dll" )]
extern static uint GetRawInputDeviceList( IntPtr pRawInputDeviceList, ref uint uiNumDevices, uint cbSize );

[DllImport( "User32.dll" )]
extern static uint GetRawInputDeviceInfo( IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize );

[DllImport( "User32.dll" )]
extern static bool RegisterRawInputDevices( RAWINPUTDEVICE[ ] pRawInputDevice, uint uiNumDevices, uint cbSize );

[DllImport( "User32.dll" )]
extern static uint GetRawInputData( IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader );

#endregion

After you add the InputDevice to your project, you can listen to events by:

// Create a new InputDevice object and register InputDevice KeyPressed event handler.

input_dev = new InputDevice( Handle );
input_dev.KeyPressed += new InputDevice.DeviceEventHandler( m_KeyPressed );

The event handler m_KeyPressed lets you to distinguish your devices through e.Keyboard.SubClass

    private void m_KeyPressed( object sender, InputDevice.KeyControlEventArgs e )
    {
        // e.Keyboard.SubClass tells you where from the event came.
        // e.Keyboard.key gives you the input data.
    }

Hope to have helped.

Community
  • 1
  • 1
  • Thanks for your help, but I am afraid your answer is a little to short for my understanding. What exactly are you doing ? – Andre Oct 30 '15 at 07:53
  • i make barcode database program for gs1 a sscc code but in.net – Ľubomír Drinka Oct 30 '15 at 10:53
  • `GetAsyncKeyState` cannot distinguish between different input devices. And it's unreliable, when used in a timer. Since it is based on snapshots, you can miss keyboard input, that happens in between consecutive sampling points. A device sending keyboard input commonly does so a lot more rapidly, than a user would. – IInspectable Oct 30 '15 at 11:59
  • Really?There are the timers.. you cannot type as fast as barcode scaner can do. input mising is theory, but practice show 10 ms you not miss any data.In few days i release program. as you my code there is only numpad keys, which can be mislead with special keyboard. You can make a code numeric lock put off and its done. – Ľubomír Drinka Oct 30 '15 at 12:57
  • The question is about filtering out keyboard input from the barcode scanner. Besides timers being low-priority messages (i.e. a 10 ms interval between samples is hardly ever met), polling is generally unreliable for observing **transient** state. Plus, the question is about distinguishing between keyboard input from different sources. Your answer doesn't address this. – IInspectable Oct 30 '15 at 13:27
  • no betweem samples but between key press. that the difference. there few people on the world who can type 400/minute = 150 ms and again barcode data is only from numeric keypad and data have always structure for example 96 100 101 96 100 101. – Ľubomír Drinka Oct 31 '15 at 06:56
  • typedef struct tagINPUT_MESSAGE_SOURCE { INPUT_MESSAGE_DEVICE_TYPE deviceType; INPUT_MESSAGE_ORIGIN_ID originId; } INPUT_MESSAGE_SOURCE; https://msdn.microsoft.com/en-us/library/windows/desktop/hh448799(v=vs.85).aspx – Ľubomír Drinka Oct 31 '15 at 07:08
  • `GetAsyncKeyState` **samples** data. You have to use an arbitrary interval, at which to sample the current state. Anything that happens in between two consecutive samples is lost. Maybe not so much of a problem, when you're dealing with user input. In case of a barcode scanner, however, you are dealing with **machine generated** input. The fact that some barcode scanners send keypad messages only, doesn't help to distinguish between barcode scanner input messages, and messages generated in response to user input from a keypad. You seem to be answering a question that wasn't asked. – IInspectable Oct 31 '15 at 10:41
  • listen i Tryed with usb wedge scaner, the code I shown from my program recognize if I typing while data is readed, because the next sub identifies start of data and than my program identiefis from which device data come. GS1 prefix 02 sscc code 00. All SDK is like that I described. – Ľubomír Drinka Oct 31 '15 at 12:09
  • The question is: How do you handle input from a barcode scanner, sent as keyboard messages, **and prevent those keyboard messages from propagating through the system?** None of your answers addresses the the second question: Identifying the keyboard input source. – IInspectable Oct 31 '15 at 12:37
  • i apologize, you have actually true. I try my program in win 7 and sometimes is part of data missing. In win XP wasn't that problem. This question have no solution as he ask. sometimes is best answer: there is no answer. – Ľubomír Drinka Nov 26 '15 at 12:44
-4

if you read my first links.

typedef struct tagINPUT_MESSAGE_SOURCE {
  INPUT_MESSAGE_DEVICE_TYPE deviceType;
  INPUT_MESSAGE_ORIGIN_ID   originId;
} INPUT_MESSAGE_SOURCE;

https://msdn.microsoft.com/en-us/library/windows/desktop/hh448799(v=vs.85).aspx POS for .NET | Differentiate between (barcode) scanner and keyboard input and data from scanner is joined can be split with vbcrlf

http://msgroups.net/development.device.drivers/how-to-uniquely-identify-a-usb-hid/14444

Community
  • 1
  • 1
  • This cannot distinguish between two different USB devices, that register themselves as keyboards with the system. The structure will hold `IMDT_KEYBOARD` and `IMO_HARDWARE` for both devices. And please, don't start a new answer whenever you want to add something. Use the [edit](http://stackoverflow.com/posts/33430967/edit) link on your previous answer, and update that. – IInspectable Oct 31 '15 at 10:35
  • https://bytes.com/topic/c-sharp/answers/262261-identifying-input-keyboard-devices – Ľubomír Drinka Oct 31 '15 at 12:52
  • [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer). This is mandatory reading. I have pointed out, time upon time, that you are ignoring the question that was asked. None of your proposed solutions seriously attempts to even address the question. – IInspectable Oct 31 '15 at 13:01
  • because your question have no solution. I give you a link that people try from 2003. It s like you want electricity in your house is which from. If you realized that, you must agree that I show is best what you can get. – Ľubomír Drinka Oct 31 '15 at 16:20
  • This is not **my** question. And the question even outlines a **robust** solution, that just happens to be somewhat complex (Raw Input to distinguish between input sources, and a keyboard hook to filter messages). Since that is already better than what you have to offer, why even bother? Not a single of your answers even attempts to identify the input source. And this is not just some nice-to-have feature, it's a core requirement. – IInspectable Oct 31 '15 at 16:36
  • i apologize, you have actually true. I try my program in win 7 and sometimes is part of data missing. In win XP wasn't that problem. – Ľubomír Drinka Nov 26 '15 at 12:42