Results 1 to 20 of 20

Thread: [RESOLVED] Check if a barcode scanner is launched

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Resolved [RESOLVED] Check if a barcode scanner is launched

    Hello VbForums
    In my application, I'm using barcode reader to scan barcodes.
    But before starting the scan, one should make sure that focus is on a given textbox. (barcode_txt)
    My question:
    Is it possible to check if the barcode scanner has startd scanning?
    so as to warn the user that focus should be on barcode_txt in case focus is elsewhere.
    your help is very much appreciated

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check if a barcode scanner is launched

    Is it possible to check if the barcode scanner has startd scanning?
    probably too late by then, most modern bar code scanners just work as a keyboard input, so it may be possible to detect some keyboard input and the active control, change the focus and warn the user to scan again, but if other controls may accept keyboard input, this will be difficult to determine
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Check if a barcode scanner is launched

    Most USB barcode scanners are discovered as HID (Human Interface Devices) by Windows. In this category you have most keyboards and pointing devices too so plugging a USB scanner you actually get a second keyboard available in the system.

    It is possible to get KeyDown event (it's called WM_INPUT as a windows message) from a HID keyboard without focusing on a TextBox control first. For instance you can get this KeyDown event on the parent form no matter if a TextBox is focused or not. Using this KeyDown event you can implement OnScannerReceive event on the parent form with the complete barcode as string coming form the particular HID keyboard.

    This way you can scan barcodes without having any TextBox controls on the form. Or you can place TextBox, ListBox, whatever controls you want on the form and scan "into" any of these whichever currently has the focus when OnScannerReceive is raised.

    Here is a sample that does this. You first configure the HID/USB scanner you want to listen to and then you start getting complete barcodes in OnScannerReceive callback.

    cheers,
    </wqw>

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    thank you gentelmen for your interest
    I read wqweto's post tens of times and I put the message in google translate trying to understand every point.
    I find this suggestion fabulous.
    This way you can scan barcodes without having any TextBox controls on the form. Or you can place TextBox, ListBox, whatever controls you want on the form and scan "into" any of these whichever currently has the focus when OnScannerReceive is raised.
    Trying to get inspired from this idea, I get half solution:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Me.ActiveControl Is Me.barcode_txt Then Exit Sub
    MsgBox " Please scan again"
    barcode_txt .SetFocus
    End Sub
    However I hope I can get what wqweto is suggesting here:
    For instance you can get this KeyDown event on the parent form no matter if a TextBox is focused or not. Using this KeyDown event you can implement OnScannerReceive event on the parent form with the complete barcode as string coming form the particular HID keyboard
    I downloaded the sample demo but I found it too advanced and I could not put it into practice.
    thank you very much

  5. #5
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Check if a barcode scanner is launched

    If your application that you are scanning bar codes into has a keyboard shortcut to move the cursor to that input field, some bar code scanners can add a prefix code to the bar code. If you add a prefix code that moves the cursor to that field, that would be a solution.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    Quote Originally Posted by jdc2000 View Post
    If your application that you are scanning bar codes into has a keyboard shortcut to move the cursor to that input field, some bar code scanners can add a prefix code to the bar code. If you add a prefix code that moves the cursor to that field, that would be a solution.
    I'm sorry sir I could not grasp this point.
    barecodes are pre established on the package.
    thank you

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Check if a barcode scanner is launched

    Quote Originally Posted by Mustaphi View Post
    barecodes are pre established on the package.
    Yes, but your barcode scanner adds a new-line character after every barcode scaned which is not included in the barcode itself.

    Most barcode scanner devices can be programmed to send prefix and suffix symbols in addition to the very barcode. By default the suffix symbol on most devices is programmed to be a new-line character.

    This explains why you get the default CommandButton clicked on your form when you scan a barcode -- it's the "Enter" symbol the device sends in addition which fires it.

    This explains why when you scan in Notepad you get each barcode on a separate line too.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    Quote Originally Posted by wqweto View Post
    Yes, but your barcode scanner adds a new-line character after every barcode scaned which is not included in the barcode itself.

    Most barcode scanner devices can be programmed to send prefix and suffix symbols in addition to the very barcode. By default the suffix symbol on most devices is programmed to be a new-line character.

    This explains why you get the default CommandButton clicked on your form when you scan a barcode -- it's the "Enter" symbol the device sends in addition which fires it.

    This explains why when you scan in Notepad you get each barcode on a separate line too.

    cheers,
    </wqw>
    that's very clear sir
    But still my concern is how I could achieve this:
    This way you can scan barcodes without having any TextBox controls on the form. Or you can place TextBox, ListBox, whatever controls you want on the form and scan "into" any of these whichever currently has the focus when OnScannerReceive is raised.
    thank you

  9. #9
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Check if a barcode scanner is launched

    If you are creating the form, you can add a keyboard shortcut that, when used, will move the input cursor to the text box where your bar code is to be entered/scanned. Then, with an appropriate scanner, set the scanner to send that keyboard shortcut prefix code before sending the bar code, and presto, when the bar code is scanned, the scanner will send the keyboard shortcut to move the cursor to the appropriate text box and then send the bar code to put into that text box.

  10. #10
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    264

    Re: Check if a barcode scanner is launched

    What about wqweto's sample was too complex for you? The interface used in the main form couldn't be more simple. Do you need instructions on how to download source code from github?

    Thanks for posting this, wqweto, it looks like an excellent approach to this problem. I guess you'd still have to deal with filtering those keystrokes from the rest of the form or making sure that no other controls used that keyboard input.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    ahenry
    please excuse my ignorance
    But how can I use this sample to fit my situation?
    In the sample, I can see a combobox holding 3 items, a texbox and a label.
    USB Device (HIDClass)
    Pilote clavier de Terminal Server (System)
    Clavier standard PS/2 (Keyboard)
    What should I exactly do?
    thanks

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Check if a barcode scanner is launched

    There are a few options some of which may depend on the type of barcode you are scanning. For example UPC codes will be numeric and fixed length.
    You can use the key preview property of the form to capture the keys no matter what control has focus and if you can determine that it was a barcode stuff the data into the correct text box. As mentioned above almost all barcode scanners have a configuration tool that can allow you to send something before and/or after the actual barcode. You can use this to your advantage.
    There are also barcode scanners that function as serial devices but they are not nearly so common now as they once where. With those you would use something like MSComm and process the data when it arrives at the com port.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    DataMiser
    the barecodes I'm using are numeric and fixed length
    You can use the key preview property of the form to capture the keys no matter what control has focus and if you can determine that it was a barcode stuff the data into the correct text box
    Could you show an example how to do that please?
    As mentioned above almost all barcode scanners have a configuration tool that can allow you to send something before and/or after the actual barcode. You can use this to your advantage.
    What I'm aware of is that the scanner is sending a CR after the scan process.

  14. #14
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Check if a barcode scanner is launched

    Quote Originally Posted by Mustaphi View Post
    What should I exactly do?
    Using Remote Desktop is a completely different story and HID devices cannot be shared with the RD session so the sample I linked above will not work in your case.

    For best results with Remote Desktop one can use serial port scanner as serial ports can be configure to be shared with the RD session. Unfortunately this means coding on your part to handle the serial ports from your application -- configure the scanner device port and listen for input.

    So basically you are at square one with your problem.

    What we have done for keyboard scanners to work over RDP was to program the devices prefix to something unique like ~

    This way when a ~ is received in form's KeyDown event we could implement a "barcode grabbing loop" with a 100 ms timeout and collect keyboard input into a string up to final new-line symbol (i.e. CR on your device).

    This keyboard "loop" is sloppy, shaky and requires configuring each device model individually (different programming barcodes from vendor documentation) but got the job done in production for us.

    cheers,
    </wqw>

  15. #15
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    264

    Re: Check if a barcode scanner is launched

    Quote Originally Posted by Mustaphi View Post
    ahenry
    please excuse my ignorance
    But how can I use this sample to fit my situation?
    In the sample, I can see a combobox holding 3 items, a texbox and a label.
    USB Device (HIDClass)
    Pilote clavier de Terminal Server (System)
    Clavier standard PS/2 (Keyboard)
    What should I exactly do?
    thanks
    Did you run the program compiled? The subclassing might not be IDE-friendly.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    wqweto said the sample does not fit my situation

  17. #17
    New Member
    Join Date
    Sep 2021
    Posts
    3

    Re: Check if a barcode scanner is launched

    Depending on your scanner, you might be able to add a character before your data is sent. So you can turn on KeyPreview on the form your using, and use a key_down, or key_press and when you detect that character you give focus to your text box.

  18. #18
    Member
    Join Date
    Oct 2019
    Posts
    37

    Re: Check if a barcode scanner is launched

    Hi,

    I done a code to select the keyboard input to a textbox2(program entry) or an external data in textbox1 (barcode):
    This code don't need a prefix character to send datas (option check), but it can easily be changed to trig both entries.

    Name:  keypreview.JPG
Views: 733
Size:  17.0 KB

    Zipped Project enclosed.
    keypreview.zip


    Have fun.

    Code:
    Option Base 0
    Option Explicit
    Option Compare Text
    
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
    
    If Check1.Value Then
        If InStr(1, "0123456789", Chr(KeyAscii)) And Text1.Text = "" Then
            If Not Text1 Is Screen.ActiveControl Then
                    Text1.SetFocus
                    Text1.Text = Chr(KeyAscii)
                    Text1.SelStart = 2
            End If
        End If
        Else
        If InStr(1, "0123456789", Chr(KeyAscii)) And Text2.Text = "" Then
            If Not Text2 Is Screen.ActiveControl Then
                    Text2.SetFocus
                    Text2.Text = Chr(KeyAscii)
                    Text2.SelStart = 2
            End If
        End If
        
    End If
    
    End Sub
    
    Private Sub Form_Load()
    Me.KeyPreview = True
    Text1.Text = ""
    Label2.Caption = "No Focus"
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then Text1 = ""
    End Sub
    
    Private Sub Text1_LostFocus()
    Text1.Text = ""
    End Sub
    
    Private Sub Text2_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then Text2 = ""
    End Sub
    
    Private Sub Text2_GotFocus()
    Label2.Caption = "With Focus"
    End Sub
    
    Private Sub Text2_LostFocus()
    Label2.Caption = "No Focus"
    End Sub
    Last edited by XavSnap; Jul 21st, 2022 at 05:23 PM.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: Check if a barcode scanner is launched

    XavSnap
    WONDERFUL
    million thanks

  20. #20
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    673

    Re: Check if a barcode scanner is launched

    Quote Originally Posted by wqweto View Post
    Using Remote Desktop is a completely different story and HID devices cannot be shared with the RD session so the sample I linked above will not work in your case.

    For best results with Remote Desktop one can use serial port scanner as serial ports can be configure to be shared with the RD session. Unfortunately this means coding on your part to handle the serial ports from your application -- configure the scanner device port and listen for input.

    So basically you are at square one with your problem.

    What we have done for keyboard scanners to work over RDP was to program the devices prefix to something unique like ~

    This way when a ~ is received in form's KeyDown event we could implement a "barcode grabbing loop" with a 100 ms timeout and collect keyboard input into a string up to final new-line symbol (i.e. CR on your device).

    This keyboard "loop" is sloppy, shaky and requires configuring each device model individually (different programming barcodes from vendor documentation) but got the job done in production for us.

    cheers,
    </wqw>
    Sounds great. Is there an example to refer to. Expert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width