Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0
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)
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 1-Sep-12 8:27am
yayaNasr1.3K
Comments
Kenneth Haugland at 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?
yayaNasr at 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 at 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?
yayaNasr at 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 )
Sourav Sarkar-SS at 1-Sep-12 15:49pm
   
This concept will not work.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
yayaNasr at 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ő at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v2
Comments
Zoltán Zörgő at 3-Sep-12 15:02pm
   
Interesting! My 5
Espen Harlinn at 3-Sep-12 18:03pm
   
Thank you, Zoltán :-D
yayaNasr at 8-Sep-12 17:40pm
   
is that a C# code
Espen Harlinn at 9-Sep-12 6:18am
   
Can easily be used from C# and I've updated my answer regarding this.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
yayaNasr at 1-Sep-12 14:57pm
   
it is a usb barcode and i did not know how can i solve my problem
Mika Wendelius at 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.
yayaNasr at 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
Mika Wendelius at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Bar Code reader automatically press enter key(keychar 13) after entering the code.
 
so you can alter your code a little bit
private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
             if(e.keyCode=Keys.Enter)
                 {
                    MessageBox.Show(e.Key.ToString());
                 }
        }
this may help you.
  Permalink  
Comments
yayaNasr at 1-Sep-12 15:25pm
   
the text box need to be focused every time that is my problem with textboxes
Sourav Sarkar-SS at 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)

  Print Answers RSS
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 407
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,632
1 Sergey Alexandrovich Kryukov 5,429
2 Maciej Los 3,474
3 Peter Leow 3,309
4 DamithSL 2,495


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 9 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100