Capture Keyboard/barcode scanner input check for duplicates?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Devsolance
Posts: 1
Joined: 22 Jun 2018, 11:31

Capture Keyboard/barcode scanner input check for duplicates?

22 Jun 2018, 11:36

So I have a problem where, i have to scan things into a field in a program, and was hoping that i could have AHK monitor keyboard/UPC scanner input and check for duplicate codes.

Currently my scanners delimit the codes with a ";" and a carriage return (Which could indicate the start of the next code, and the ; indicating the end of the code?) at the end of each scan, so i could parse each code maybe if i dump all input to a file then run a sort on it?

Example input from scanner/keyboard interface
J93KNP891;
KH973626NP;
BN21234NOP;

Any suggestions would be super helpful, point me in the right direction and hopefully i could walk the rest of the way myself!!

Thanks so much for your time
Dev
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Capture Keyboard/barcode scanner input check for duplicates?

22 Jun 2018, 22:13

Hell Devsolance.
Welcome to the AHK-forum.

Your issue is easy to solve. As your list has semicolon and carriage return `r you can choose between at least two ways.
(I think it must be linefeed `n, so following examples use linefeed - you can easily change with carriage return `r, if I am wrong)

Way 1: Delete all semicolon and sort with linefeed as delimiter.
Way 2: Delete all linefeed and sort with semicolon as delimiter.

The sort-command can even delete duplicates. See example code for way 2 below:

Code: Select all

Fileread, myList, c:\german_teacher_recommendation\bullshit\ScanCode.txt ; read content into variable called myList
StrReplace(myList, "`n") ; replace all linefeed
Sort, myList, d; U ; determine delimiter with parameter d and remove duplicates with parameter U
msgbox % myList ; just for testing - display results
Way 1 is similar, just change `n with ; and vice versa.

If not, just sit and hear Andrew Eldritch rocking. :mrgreen: :mrgreen:

EDIT: Way 3 - It is even possible, to directly sort with carriage return as delimiter, without replacing the semicolons.
Einfach nur ein toller Typ. :mrgreen:
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Capture Keyboard/barcode scanner input check for duplicates?

25 Jun 2018, 08:02

Stuff it into an associative array - key names must be unique, so that will automatically filter out the duplicates - no need to search through them.

Code: Select all

scanList =
(
J93KNP891;
KH973626NP;
BN21234NOP;
BN21234NOP;
)

; Remove all the ; characters...
StringReplace, scanList, scanList, `;, , ALL
; Split the string into lines
scans := StrSplit(scanList, "`n")

; Now build an array using name as the key.
; Key names are unique, so this will filter out the duplicates
filtered := {}
for i, chars in scans {
	filtered[chars] := 1
}

; Now re-build the list
list := ""
for name, unused in filtered {
	list .= name "`n"
}

msgbox % list
FYI, when working with scanners and AHK, I found it incredibly useful to configure the scanner to send a "preamble" key *before* it sends the scanned characters.
This is usually done by scanning special barcodes from the manual.
Lets say it sends F12 as a pre-amble.
Declare an AHK hotkey to F12 which activates an "input" command which waits for keys to be sent, and waits for the end char (;)
https://autohotkey.com/docs/commands/Input.htm

This may be complicated as your "End Char" is ;, which is a comment in AHK, but here is roughly what your code would probably look like:

Code: Select all

F12::
	; "Start waiting for input, end when you see ;"
	Input, chars, ,;
	; chars now holds the characters scanned
	; Pass the characters on to your processing function
	ProcessScan(chars)
	return
I have some code that I wrote when working in asset management - it may be useful: https://autohotkey.com/boards/viewtopic.php?t=4686
Last edited by evilC on 25 Jun 2018, 08:32, edited 3 times in total.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Capture Keyboard/barcode scanner input check for duplicates?

25 Jun 2018, 08:11

Also, if you have a USB scanner which masquerades as a USB keyboard (most do), then it will have a unique VID/PID, which will be different from your normal keyboard.
AutoHotInterception (See link in signature) can be used to filter input coming from a *specific device* - ie it will allow you to differentiate keys coming from the scanner from those coming from your regular keyboard.
It requires installing a custom driver though, so the preamble technique is maybe preferable - but if you can't alter the preamble, then AHI could negate the need for it ;)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bobak, MrDoge and 234 guests