Differentiating input from 2 separate keyboards - c#

All,
Apologies in advance if this is an ill-informed question with lack of detailed examples of what I have tried to do so far - all the material I am finding on this topic seems to be out of scope of what I'm trying to do, or its just going right over my head (Learning resources on this are welcome!!).
What I am essentially trying to do is build a small program that will differentiate the input device used to create a user input on my Lenovo Win10 Machine that can run in the background and trigger scripts when certain conditions are met. In my case the input devices are a generic wireless/USB keyboard, and a USB Barcode scanner that acts as a HID Keyboard (The Barcode Scanners plugs directly into the machine, not via a keyboard).
The reason I need to differentiate the input is for example, if I have a script run every time an asterisk is printed from a scanned barcode I want the user to still be able to type an asterisk from the keyboard without triggering the script - Im restricted to using Code 128 barcodes so I also cant bypass this issue by adding characters to the barcodes to trigger the scripts that are not present on the keyboard.
In pseudo-code its essentially
start:
if inputdevice == keyboard 1
then
System.Diagnostics.Process.Start(myBatchFileName);
else
goto start;
Initially I tried to do this in VB, using some code I had previously used to listen to a COM Port to record output from an arduino -
Dim barcodescanner As SerialPort
barcodescanner = New SerialPort()
barcodescanner.PortName = "COMx"
barcodescanner.BaudRate = 9600
barcodescanner.Parity = Parity.None
barcodescanner.DataBits = 8
barcodescanner.StopBits = StopBits.One
Dim barcode As String
barcodescanner.Open()
barcode = barcodescanner.ReadLine()
barcodescanner.Close()
-however for reasons beyond me I cannot successfully use a virtual COM port for the scanner and record data this way, I also tried to create a virtual COM port using drivers from http://www.ftdichip.com/FTDrivers.htm but this was to no avail.
From some investigation I found some C# Classes, namely; InputEventArgs, InputDevice, RoutedEventArgs, that seem as if they could be used to resolve a device ID from an Input/Event that may allow be to harness some information I could use to flesh out and build the pseudo IF THEN ELSE loop above - but im struggling to use the classes and actually build any working code.
Does anyone have any suggestions as to how to do this?
In a perfect world if I could create a virtual COM port for the barcode scanner and listen to it using VB that would be ideal - but if not could anyone suggest a method of doing this in C# or point me to another method?

The problem is that a keyboard-emulating scanner and a keyboard work the same way and Windows does not allow you to find out which device actually sent the keystroke.
One way would be (if possible for your scanner):
Configure the scanner to work as COM port emulation (not possible with all scanners, only industrial ones support this normally)
Install the manufacturer's virtual COM port driver if necessary
Then you can actually open a COM port and communicate with the scanner as desired - you then, however, need to put the scanned strings into text fields yourself if required.
Second possible solution (if available for your device):
Configure the scanner to transmit a certain prefix or suffix along with the barcode content (most scanners can be configured to send a barcode identifier before the content, which is highly unlikely to be typed by the user)
Also configure the scanner to send CRLF or CR as last character
When you receive CRLF or CR, check whether the input starts with that prefix/ends with that suffix
If it does, remove the prefix/suffix from the text box and react to the "*" as desired.

Related

How do I use a device exclusively in my c# application?

First of all no I'm not writing a keylogger... what I have is a barcode scanner that appears as a keyboard... well I need a way to use the scanner exclusively... this app will run in the background so I need ALL scanned data to scan into my app regardless of what app I'm using..
my guess is to make an invisible form that pops up and has focus when I pull up the screen that I need the barcode scanned..
Please take a look at my answer here How HID devices work when programming?
You can set up your scanner to function as a USB-HID device and capture PDF417 scanned data.
Don't know if you need to do anything special. From what I have worked in the past, scanners are not much more than a keyboard wedge device. Whatever field you are in, scan and as long as a simple barcode (code 39, code 128, similar) that are not complex, they should just fill in whatever field you are on.
Now, that being said, if you have a 2d barcode (PDF417), or other complex barcode that has capacity of larger amounts of data, special characters, etc., that you may need to do parsing, that goes an extra level.
If you could also provide maybe the specific device / manufacturer, there might be libraries that you can use to pre-capture the data and parse before sending forward.
FEEDBACK per comment.
I had to do a similar option of licenses, but limited states needed. Only a few states actually provide their specific format. Doing hospitality work, you probably need to cover most states, and probably skip over international and require that.
Also, having to push the results into whatever current app is, currently active, you might be able to capture in the background and push a Windows message of the final decoded to the active application. May need to dig into those API calls more directly.. Good luck, but I'll try to look into possible messaging for you too.

Scanner driver conflicts with 2 same scanners

I have this C#, WIA app that I made to control 1+ scanners. Its on Win7
I start a new instance of the app each time I want to control another scanner. This works fine with 2 different scanners, HP and Epson, but when I add another Epson and try to scan with it, it fails. I believe this is due to the drivers being locked by the first Epson scanner. How do I tell the second Epson scanner to use a different set of drivers? My thoughts are to copy the first scanners drivers to another location. Then search the registry for the first set of driver files. Then see if I can puzzle out individual scanners. I know that there is a unique ID property for the scanners. Then i would tell the second scanner's ID to use these drivers instead.
I have thought about using a Virtual Machine but don’t want to do that.

few questions concerning usb barcode scanner

this is probably an old question, but I've read through articles about usb barcode scanner and still can't make my mind about how to use it.
I'm using an USB barcode scanner, and to my understanding there are two methods to use it - as keyboard wedge and serial port.
correct me if i'm wrong, but serial port might be easier to use (because there is an distinction between the scanner's input and the keyboard's) but you must hold info about the port, which might be problematic since I can't tell which port the scanner will get on clients' computers. Is that correct?
Second question- if I will go with the keyboard wedge option, I need to tell the scanner's input from the keyboard's. In that case I need to focus on a textbox, and know when the scanner's input begins and ends. There is an option to configure the scanner to send special character at the start/end of the stream. If I don't have control over the client's scanner type and configuration, how can I tell that the specific input is the scanner's?
Thanks ahead
If you have a serial (RS-232) scanner you have to configure in which port number your program listen , usually through the serial port you have some advanced command that you can send to the scanner for enable or disable reading, and so on ...
A Scanner connected through USB can be configured in wedge mode and you have to autofocus on the textBox that you want to fill, optionally some model of scanner can be configured for reading barcode with a prefix or/and a suffix char. It depends on your scanner model .
Have you considered Microsoft POS for .Net? The framework allows you to subscribe to events in C# and then you don't have to worry about setting focus on TextBoxes or the user clicking whilst scanning, etc.
The scanner to be set to IBM-USB and (I think) has to support "Control Objects" to use it with the POS for .Net stuff. An example of one that works well is a Datalogic QuickScan 6500.

Keyboard Emulating barcode reader

I'm using the Microsoft.PointOfService libraries to interact with POS hardware. Although documentation about compatible hardware is thin on the ground, I've read in places that all keyboard emulating barcode scanners should be compatible.
so now I have my barcode scanner (Wasp WSR-455), which emulates the keyboard, and I was wondering if anybody here knows how to make the posdm tool recognise it, or another way I can program against a barcodeScanned event.
D.R
You don't bother with that, nor can you, when they emulate a keyboard. Either you or the user has to make sure the focus is always on a specific text box so the scanner output goes to the right place. This certainly removes a great deal of flexibility in your UI, you'd really do need a scanner that you can connect to directly over, say, a USB port. They usually emulate a serial port, you'd use the SerialPort class.

"Symbol" brand barcode scanner and c#

I have a "Symbol" brand barcode scanner (USB) and I am trying to capture the data it scans from a barcode into my application (which is running as a service -- with no textbox control, of course).
The catch is that : Whenever you scan something, it acts like a keyboard and outputs the digits to anything that has focus (i.e notepad, word, etc).
My question is: How do I perform this barcode scan in the background and put it in a variable that I can use in my in C#.
So far, the only api's that I've found are for the .NET CF and I need this to be a
windows service.
Basically, I want to be able to send certain keystrokes to an application if the barcode = "123456789-0111" without interfering with the current window that has focus. After my application reads the barcode in then it will look for a certain program (launch it and set focus) and send keystrokes. I am using Code 128.
The HID mode will be your best best.
Put the barcode reader in HID mode and make your service capture the reader. When data comes in take a look at it and if it is one of your special barcodes you can act on it.
If the data is not one of your special barcodes then inject them as keydown/keyup windows messages so that it will seem to work in the same fashion as in keyboard emulation mode.
Put the scanner into USB mode. This is generally done by scanning a series of barcodes in your manual or printed out from the software provided from symbol.
With the scanner plugged in/docked connect to windows update. You'll get an update specific to that scanner. It won't work if if the scanner is not plugged in.
The scanner should map to a com port. This has typically been com 2 on the machines I've set it up on.
Download JustinIO. For my scanner, useful COM settings: Baud Rate: 9600, ByteSize 8, StopBits: 1 Parity: 0
Create a new CommPort instance. Set up a thread that does something like:
StringBuilder sb;
byte[] b;
do
{
b = commPort.Read(1);
sb.Append(Encoding.ASCII.GetChars(b));
Thread.Sleep(20); //symbol is slow...
} while(b.Length > 0);
Note: that's much crunched down from the code that I'm actually using so you'll need to modify it for your application.
Send the string to your application via whatever method seems appropriate.
The scanner is configured as HID (human interface device) and emulating keyboard.
To access it through Symbol's API, you probably have to disactivate current communication mode and set it to native one - should be documented in the scanner manual. Then you can access it directly without input field.
PS. I have no Symbol's scanner, but this is common to many devices.
It really depends upon the barcode scanner's api, and that is specific to the vendor. You'll need to get the the api for the scanner from the vendor, if you can't find it on their site you should just email their support, though you might find you have to buy the api.
First..
Ignore the .NET CF info for now as it probably pertains to the actual handheld unit. The mode of operation, pretending to be a keyboard, is referred to classically as a 'keyboard wedge'.
Second..
What kind of barcode symbologies are you attempting to use? And for what purposes? Or, mor directly,...What do the barcodes look like when scanned? (Do they contain special leading and trailing characters?)
Third... keep it simple... avoid the API unless it is necessary...
In the past when I've had to interface with barcode scanners, I've used a very simple approach in that my data entry form has the property of KeyPreview true and contains a method to detect and direct input originating from the scanner (by examining starting and trailing characters) to the appropriate control. Most of the scanners I've dealt with were programmable (usually via barcodes) to pass a custom set of characters to signal beginning and end of scanned input.
I hope that helps....
Here is a CodeProject sample that demonstrates how to set up global mouse and keyboard hooks in C#:
http://www.codeproject.com/KB/cs/globalhook.aspx
You could add this code to your console application, and then just monitor all keyboard input, looking for the specific codes you need. No API fuss or muss.

Categories

Resources