If you want to allow any barcode format and distinguish between actual keyboard input, your best option is to add a prefix and suffix character to the scan by programming the scanner to do so.
For example, you scan 1234567
your scanner will append a prefix and suffix of your choice, like, $1234567*
. Then the component knows when to capture input as a scan because it is framed with $
and *
.
Regex is more suited to filtering scans to a specific pattern, "I need only this barcode format to be picked up - A-123-002-1
"