How to retrieve a string in a C# Key(*)Event that was inserted by scan? (Windows CE 5)

1k Views Asked by At

I am working on a Win CE 5 application that captures data scanned via barcode scanner. The application should support some "system-barcodes", predefinded barcodes that trigger functions of the application.

Those barcodes have to work in the whole main-frame. So I set the property "KeyPreview" of the main-frame to true and registered an KeyEventHandler on the KeyDown event of the main-frame.

My problem is, I couldn't figure out how to get the full scanned string. The string is 12 characters long and ends with a newline. Is there a possibility to get the whole string in the EventHandler?

Thank you in advance for any help.

2

There are 2 best solutions below

0
On BEST ANSWER

Most, if not all, barcode scanners based on Windows CE inject the scan data as keyboard data to the device driver. The easiest way to intercept that data in your app, where you don't have to hook up handlers to every form and worry about controls getting the keys before your processing logic is to use a keyboard hook. It works just like on the desktop, so any code you find that applies to keyboard hooks on the desktop would be valid (with the exception that the DLL containing the APIs in WinCE is coredll.dll).

There is an example of keyboard hooking for Windows Mobile on CodeProject which would probably give you the bases for everything you need. From there, it's simply string parsing in the hook handler.

1
On

Using any of the Key events by setting the Form.KeyPreview property should allow you to see the data from the scanner. But you will only see it one "key" or character at a time. Those events only handle a single character or key on each call. As far as you app could tell, it looks no different than a user smashing keys on the keyboard.

If you have the option, and can put your scanner into a non-keyboard emulation mode and hook into scan events directly, then you would probably be much more satisfied with the results in terms of how it can work independently of the GUI when used that way. Typically with devices that can do that, you will receive the entire scan as a single event.

I'd suggest including the details about the device(s) you are using so someone might be able to give you more specific advice that might be relevant for the hardware in question.