Click here to Skip to main content
15,867,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys ,
i am developing a wpf application that use bar code reader , i want to get the code when the bar code has been scanned the code i create an key down event and try to get the keys that entered but only i get the first key how can i get all the code (all the pressed keys)
C#
private void Grd_Main_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.Key.ToString());

        }


can any one help me in doing that ?
thanks an advanced ...
Posted
Comments
Kenneth Haugland 1-Sep-12 14:30pm    
What?!? Perhaps you should wait for the check until all the chars are read?
I thought they came in known sizes, no?
Yasser El Shazly 1-Sep-12 14:33pm    
i think it is 12 char , but how can i do that i think the event fired once the first key is pressed and ignore the other keys right me if i wrong
Kenneth Haugland 1-Sep-12 14:38pm    
something like: txtBox1.text.tostring.length = 12 then ... and print the complete text. I dont know what Grd_Main_KeyDown is though I suspect it is some kind of datagrid?
Yasser El Shazly 1-Sep-12 14:43pm    
if i used textbox i must be sure it is focused and active be fore i scan so the user must give it the focus by mouse click or keyboard tab and i did not want that so i created this event for the MainWindow if the window active the event will be fired (no need to mouse click or keyboard tab )
[no name] 1-Sep-12 15:49pm    
This concept will not work.

Typically there is an ending character, that can be configured on the reader. Can be a special one or Tab, Enter. But if you are using a general barcode reader, you will never know if a user pressed a key, or it came from the reader.
But as the reader is just an other HID device, you should be able to handle it as a separate keyboard. Check here how: Using Raw Input from C# to handle multiple keyboards[^]. This way, you will be able to handle differently the keys coming from the actual keyboard, and the reader.
 
Share this answer
 
Comments
Yasser El Shazly 1-Sep-12 15:01pm    
i just want to create a specific event fired only when the character come from the reader and get all the character to get this code and select it's corresponding data from mu data store that is all my problem
Zoltán Zörgő 1-Sep-12 15:06pm    
Than try out the code from that article. Should work, but probably you will need to change the code part that searches for keyboard devices in the registry to find also your reader.
You need to use something like Microsoft Point of Service for .NET v1.12 (POS for .NET)[^]

Microsoft Point of Service for .NET v1.12 (POS for .NET) is a class library that enables POS developers to apply Microsoft .NET technologies in their products. It provides a simple and consistent interface for .NET Framework applications to interact with POS devices, in compliance with the UnifiedPOS standard (aka UPOS). In addition, it provides windows plug and play (PnP) support. There are thirty-six classes of POS peripherals supported including cash drawer, receipt printer, barcode scanner, magnetic strip reader (MSR), line display, RFID, Biometric, Belt, Check Scanner, Fiscal Printer, Electronic Journal, Image Scanner, Item Dispenser, Magnetic Ink Character Recognition Reader (MICR), PIN Pad, signature capture device, etc. In addition, POS for .NET supports many legacy devices where OPOS service objects are available.

This is considered the preferred way to interact with POS devices from .Net.

You can also have a look at the UnifiedPOS page[^]

In case you are using a Symbol scanner you can download the opos driver from
here[^]

Update

Microsoft Point of Service for .NET v1.12 is a set of .Net assemblies, all you need to do is to add a reference to the assembly.

OPOS is COM based, so you need to import the type library for the dll into .Net, which you can do in nearly the same manner you import .Net assemblies.

So both technologies are easily accessible from C# and .Net.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Zoltán Zörgő 3-Sep-12 15:02pm    
Interesting! My 5
Espen Harlinn 3-Sep-12 18:03pm    
Thank you, Zoltán :-D
Yasser El Shazly 8-Sep-12 17:40pm    
is that a C# code
Espen Harlinn 9-Sep-12 6:18am    
Can easily be used from C# and I've updated my answer regarding this.
First of all as Kenneth already pointed out, gather all the characters.

For example if the length of the code is 12 characters, create a string variable on window level and accumulate the individual characters into it. When the length of the string is 12, you have your code.

Second thing, if you want to get the code regardless where the focus is, take advantage of bubbling routing. What I mean is that you can for example define the KeyDown on the Window solely so regardless of the current focus on the window, the event would be handled in a single place. This may or may not be a feasible strategy depending on the UI and the requirements and how you recognize the barcode.
 
Share this answer
 
Comments
Yasser El Shazly 1-Sep-12 14:57pm    
it is a usb barcode and i did not know how can i solve my problem
Wendelius 1-Sep-12 15:00pm    
The physical connection is USB but it still emulates key strikes, correct? If that is the case, it doesn't differ much from input directly from the keyboard.
Yasser El Shazly 1-Sep-12 15:02pm    
i just want to create a specific event fired only when the character come from the reader and get all the character to get this code and select it's corresponding data from my data store that is all my problem
Wendelius 1-Sep-12 15:06pm    
Ok, so basically you want to know if the input came from the barcode reader or did it come from the keybord. Is that correct? Is there anything in the beginning of the barcode that would help you to identify that the data is coming from it? Otherwise I think the link Zoltán Zörgő gave is the correct way to go.
Bar Code reader automatically press enter key(keychar 13) after entering the code.

so you can alter your code a little bit
C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
             if(e.keyCode=Keys.Enter)
                 {
                    MessageBox.Show(e.Key.ToString());
                 }
        }

this may help you.
 
Share this answer
 
Comments
Yasser El Shazly 1-Sep-12 15:25pm    
the text box need to be focused every time that is my problem with textboxes
[no name] 1-Sep-12 15:47pm    
ya, for that you need to write the code to focus it every time you need to enter the barcode.
use textBox1.Focus(); the TabIndex of the textBox should be zero this ma also help you.

OK I read your comment but that concept will not work for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900