Recent

Author Topic: Help with barcode scanning  (Read 550 times)

cursosba

  • New Member
  • *
  • Posts: 13
Help with barcode scanning
« on: November 30, 2023, 10:53:50 am »
Hello, I have a small application that reads employee registration numbers from a barcode on a badge, the registration number is saved in the bank with 12 characters, example: 000000002014, and the barcode is generated: 0000000020145 EAN13 format, in form I have a radiogroup with the manual and automatic option, if you enter 0201 and click on save it is completed with 0 on the left until the correct registration is formed, if you type the 12 characters it reads directly and if you see the 13 in the barcode it is removed the digit leaving only the 12. my problem is reading the barcode, below is the code I use in the edit's onkeydown, however if I press crtl+v it works, but if I read the barcode with the scanner comes with 12 digits and does not work.

Code: Pascal  [Select][+][-]
  1. procedure TFrmPrincipal.Edit1KeyDown(Sender: TObject;
  2.   var Key: Word; Shift: TShiftState);
  3. var
  4.   registration: string;
  5. begin
  6.   if RadiobuttonAutomatic.Checked then
  7.   begin
  8.     if Key =  VK_RETURN then
  9.     begin
  10.        registration := Edit1.Text;
  11.  
  12.       if Length(registration) < 12 then
  13.       begin
  14.         registration:= StringOfChar('0', 12 - Length(registration)) + registration;
  15.         ProcessBarCode(registration);
  16.       end;
  17.  
  18.       if Length(registration) = 12 then
  19.       begin
  20.         registration:= StringOfChar('0', 12 - Length(registration)) + registration;
  21.         ProcessBarCode(registration);
  22.       end;
  23.  
  24.        if Length(registration) > 12 then
  25.        begin
  26.         registration := ExtractRegistrationToEAN13(registration);
  27.         ProcessBarCode(registration);
  28.        end;
  29.       end;
  30.  
  31.     end;
  32. end;  
  33.  

Handoko

  • Hero Member
  • *****
  • Posts: 5106
  • My goal: build my own game engine using Lazarus
Re: Help with barcode scanning
« Reply #1 on: November 30, 2023, 01:55:00 pm »
I think you should put that code in the edit's OnChange event. Because KeyDown event won't be triggered if you do ctrl+v.

kwyan

  • New Member
  • *
  • Posts: 23
Re: Help with barcode scanning
« Reply #2 on: November 30, 2023, 02:03:44 pm »
I notice you check if it is VK_RETURN (when user press ENTER) and then process. Just wonder if you paste data, it will not work unless you also press Enter after Paste. Then you can check if the Barcode scanner has been configured to send ENTER after the 13 digits (and if not, your logic won't work).

Handoko

  • Hero Member
  • *****
  • Posts: 5106
  • My goal: build my own game engine using Lazarus
Re: Help with barcode scanning
« Reply #3 on: November 30, 2023, 04:16:10 pm »
@cursosba

Sorry I didn't read the question properly. You mentioned clicking the save, so does it mean there has a save button? If yes, I think you should do the code in the btnSave.Onclick event.

Something like this:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     CheckBox1: TCheckBox;
  17.     Edit1: TEdit;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.FormCreate(Sender: TObject);
  32. begin
  33.   CheckBox1.Caption := 'Auto';
  34.   Edit1.Text        := '0201';
  35.   Button1.Caption   := 'Save';
  36. end;
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. var
  40.   l: Integer;
  41. begin
  42.   l := Length(Edit1.Text);
  43.   if CheckBox1.Checked then
  44.   begin
  45.     if l < 12 then
  46.       Edit1.Text := StringOfChar('0', 12 - l) + Edit1.Text;
  47. //  if l > 12 then doExtractRegistrationToEAN13
  48.   end;
  49.   ShowMessage(Edit1.Text);
  50. end;
  51.  
  52. end.

 

TinyPortal © 2005-2018