Block barcode input

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Block barcode input

Post by x-intercept » 05 Oct 2022, 03:56

The code below, which is from another thread, identifies input from a barcode reader and saves it as a variable. When I use it in my text editor the barcode input is inserted as an alphanumeric string, because the barcode reader is set up to append a carriage return after reading the barcode. Without re-programming the barcode reader, is there some way to block the input from the reader but retain the barcode string as a variable?

Code: Select all

#SingleInstance force

Chars = 0123456789 abcdefghijklmnopqrstuvwxyz!""$`%^&*()_+=-``\|,./;'#[]{}:@~<>?
Loop, Parse, Chars 
{
	fn := Func("BarcodeHandler").Bind(A_LoopField)
	Hotkey, % "$~" A_LoopField, % fn, on
}
  
Chars_Upper = ABCDEFGHIJKLMNOPQRSTUVWXYZ
Loop, Parse, Chars_Upper 
{
	fn := Func("BarcodeHandler").Bind(A_LoopField)
	Hotkey, % "$~+" A_LoopField, % fn, on
}

fn := Func("BarcodeHandler").Bind(" ")
Hotkey, $~Space, % fn, on

BarCodeHandler(key){
	global Accu
	Accu .= key
	If (Strlen(Accu) > 4 && A_TimeSincePriorHotkey < 20)
		SetTimer, TheBigShebang, -20
	Else If (A_TimeSincePriorHotkey > 25)
		Accu := SubStr(key, 0)
}

TheBigShebang:
If Accu
  msgbox You scanned barcode %Accu%
return

^Esc::ExitApp

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Block barcode input

Post by boiler » 05 Oct 2022, 05:34

Try removing the ~ character from each Hotkey command’s first parameter.

By the way, I don’t know why your line that assigns characters to the chars variable includes a space since that gets added as a hotkey specifically later, and I don’t know why it has two quotation marks in a row. Quotation marks aren’t escaped by preceding it with another one in legacy syntax, which is what is used there.

x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Re: Block barcode input

Post by x-intercept » 05 Oct 2022, 05:41

Thanks, I will try that. I have just used the code posted here:

viewtopic.php?t=60613

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Block barcode input

Post by boiler » 05 Oct 2022, 06:06

I don't see a version there that has a space between the 9 and the a like the code you posted has.

I see they do have a pair of quotation marks, but that's a misapplication of Escape Sequences. As is shown on that page of the documentation, two consecutive quotation marks are for expressions only, so there should only be one in that line. This demonstrates the difference:

Code: Select all

MsgBox, Legacy syntax requires only one " character
MsgBox, % "Expressions require the "" character to be escaped"

x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Re: Block barcode input

Post by x-intercept » 05 Oct 2022, 16:06

Thanks, boiler. Would you mind explaining to a beginner what the function of the ~ is, in the context used?
The error with the space after the 9 was my own :(
The code now does what I asked, but how can I get it to unblock keyboard input after the barcode has been recognized and the variable saved?

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Block barcode input

Post by boiler » 05 Oct 2022, 19:04

x-intercept wrote: Would you mind explaining to a beginner what the function of the ~ is, in the context used?
~ is a hotkey modifier symbol that prevents the hotkey from blocking its key’s native function.

x-intercept wrote: how can I get it to unblock keyboard input after the barcode has been recognized and the variable saved?
You can use Suspend after the input from the barcode reader is complete to make all hotkeys inactive. Use Suspend, Off when you’re ready to have it read the barcode input again.

x-intercept
Posts: 10
Joined: 05 Oct 2022, 02:58

Re: Block barcode input

Post by x-intercept » 06 Oct 2022, 05:00

Thanks!

Post Reply

Return to “Ask for Help (v1)”