Script doesn't perform correctly when hotkey is executed via barcode scanner

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
794
Posts: 11
Joined: 03 Jun 2016, 15:43

Script doesn't perform correctly when hotkey is executed via barcode scanner

27 Jun 2016, 11:00

Alright so I created a script that simply uses the "`" (non-shift key for ~) and "|" (shift version of \). I had to keep it that simple because my barcode scanner doesn't have software that can read things such as Function keys.

So I have a very basic script that looks like this (used in Excel):

Code: Select all

Send, {UP}{F2}{HOME}
Sleep, 50
Send, {DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}
Send, {SHIFTDOWN}{TAB}{SHIFTUP}
If I press one of the execution keys for the AHK on the keyboard, it works fine and operates as expected every time. However, if I scan a barcode that is supposed to enter that character, it does something completely different.

Any reason for this? Workarounds? Any help is appreciated.
794
Posts: 11
Joined: 03 Jun 2016, 15:43

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 10:31

Bump? Would really appreciate some help here. Thank you.
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 10:47

{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE} BTW, you can replace this with {DELETE 10}

I am not 100% sure what you mean.
So when you pull the trigger, the scanner types ABCD

But if you declare a hotkey to A, it does not trigger?

Strip out all your sends and make sure that the hotkey is triggering correctly first.

If you explain your use-case a bit more, we can maybe suggest ways of doing what you want.

In general, when working with barcode scanners, the "Preamble" and "Postamble" settings (Which cause the scanner to send specific keys before and after the scan) are extremely useful.

Also, {SHIFTDOWN} and {SHIFTUP} are not valid keys. You want {SHIFT DOWN} and {SHIFT UP}
794
Posts: 11
Joined: 03 Jun 2016, 15:43

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 11:04

Ah yes, I have the numbered way of inputting a key multiple times on other scripts but not this one for some reason.

{SHIFTDOWN} and up are actually both valid, as I use these on several scripts and they work as expected.

I have two scripts, one triggered by "|" and one by "`". (As seen in the OP.)

In excel, it simply moves one cell higher, selects it as editable by pressing F2, goes to the start of the text and deletes the first 10 characters. It then moves one cell to the left.

When I scan either of my activation symbols without the script running, it types the character as normal. If I press the activation symbol on my actual keyboard, it activates the script properly. But when I scan the activation symbol with my barcode scanner, it never moves a cell above (the very first step), and then moves either 1 or 2 cells to the left. (It's inconsistent.) Doesn't make sense to me. When I used the autohotkey script recorder using the barcode scanner, it didn't pick up any "preambles".
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 11:11

Maybe the barcode scanner is sending extra keys (eg a pre or post-amble) which you are not seeing, and these are interfering with the Send?
Before you start sending keys to excel, try Blockinput, On, then do a Blockinput, off once you are done sending keys

Huh, you learn something new every day, didn't know shiftdown was a valid key name.

What happens if you use different trigger characters (eg things which do not require shift)
794
Posts: 11
Joined: 03 Jun 2016, 15:43

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 11:50

Now this is very strange.

So first, I figured out why it wasn't working properly. That's because the scanner is set to press "enter" when scanned. This something I can't change, since I make heavy use of it.

For some reason the "|" (shift + \) was making the shift key stick down until pressed on the keyboard once.

"`" doesn't use shift, so I put that one in. And strangely, with either solutions, the script doesn't always perform as expected. For every 10 scans, it will perform as it should 5-8 scans. Very odd, don't know why it's being so inconsistent.
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 13:19

Bear in mind that if a hotkey is triggered that requires shift, when the hotkey fires, AHK considers shift to be held, and if you ask it to send a character that does not require shift, it will release shift, send the character, then hold shift.

Code: Select all

|::	; | is a shifted character
	Send {a}	; This will RELEASE shift, then send a, then HOLD SHIFT
	Send {A}	; This will not release shift, as A is a shifted character
	return
Probably the best way to solve this is:
Set a PREAMBLE for your scanner (Probably done by scanning a special barcode from the manual) of a non-standard character that will never normally be hit (eg F24) and is ideally non-printing (character does not appear when you hit that key).

Code the AHK script such that when it sees this key go down, it blocks the postamble (eg enter) until it sees it get hit.
This is a very useful technique for scanners + AHK, it lets ahk know when a scan starts and stops.
Last edited by evilC on 29 Jun 2016, 13:23, edited 1 time in total.
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

29 Jun 2016, 13:22

example code, assuming F24 is your pre-amble, and Enter is your post-amble.
~(Untested code, just to give you an idea of what i mean)

Code: Select all

F24::
	hotkey, Enter, PostAmble, On
	return

PostAmble:
	hotkey, Enter, off
	return
User avatar
RAFACN
Posts: 12
Joined: 20 May 2016, 10:06
Location: BRAZIL

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

30 Jun 2016, 17:01

i think you are loosing characters because of the velocity of barcode scanner. It sends a lot of characters like "01561012|asasdcva`asdas|fasdfasdf" and this script you created doesnt "buffer" this to send correcly to excel. if you interact too fast with excel by send command, excel cant respond at the same speed, so it looses characters. Autohotkey is very fast.. sometimes you must slow down it.
For this problem of yours, for exemple, you could create a persistent inputbox that would receive your scans and everytime it receives an ENTER (usually the last character send by barcode scanners) it would process the text as you want (changing characters, triggering functions, ..). For exemple, you could do this: your inputbox would receive the text, then hide (when pressed ENTER), activate excel, send the commands at the right speed, and then show again to receive you others scans. Sorry my english.. did you understand?
User avatar
RAFACN
Posts: 12
Joined: 20 May 2016, 10:06
Location: BRAZIL

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

30 Jun 2016, 17:56

I made you a code.. look at it. :D

#a hotkey calls the main script (infinite loop.. to exit, write "exit").

For debbugin purposes:
#z hotkey is just for you to test...
#q hotkey is for you to discover you real excel title..
#ins hotkey is for you to reload the script whenever you want

The routine that you have to change is My_Routine().. there you put your {DELETE 10}{ENTER}{...}.

Change "WRITE_YOUR_EXCEL_TITLE_HERE" to your excel real title. if you dont know for sure it, go to your document and press #q.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#MaxThreadsPerHotkey 2

#a:: ; WINDOWS KEY + A
{
	Title := "WRITE_YOUR_EXCEL_TITLE_HERE"
	Loop ; INFINITE LOOP, UNTILL YOU WRITE "EXIT" AND PRESS ENTER
	{
		scannedcharacters = ; CLEAR VARIABLE
		scannedcharacters_UPPER =
		inputbox, scannedcharacters, SCAN NOW, TYPE "EXIT" TO QUIT ; OPEN INPUTBOX AND CLOSES IT WHEN ENTER KEY IS SENT OR WHEN A BARCODESCANNER SENDS IT LAST CHARACTER THAT IS USUALLY AN ENTER.
		StringUpper, scannedcharacters_UPPER, scannedcharacters  ; verify if you want to exit script
		if(scannedcharacters_UPPER == "EXIT") ; verify if you want to exit script
		{
			reload
		}
		rRafa_WinActivate := Rafa_WinActivate(Title) ; GARANTEE THAT EXCEL DOCUMENT IS GOING TO BE ACTIVATED
		if (rRafa_WinActivate == "OK")  ; GARANTEE THAT EXCEL DOCUMENT IS GOING TO BE ACTIVATED
		{
			My_Routine(scannedcharacters)
		}
		else ; IF NOT, STOP
		{
			msgbox, ERROR: Excel not detected! `n`nIF YOU DONT KNOW THE TITLE OF YOU EXCEL DOCUMENT, ACTIVATE IT AND PRESS #Q (WINDOWS KEY + Q).
			reload
		}
	}
	return
}
return

#z:: ; WINDOWS KEY + Z ; TEST
{
	scannedcharacters := "abcde|fghij| klm`opqx"
	My_Routine(scannedcharacters)
	
}
return

My_Routine(scannedcharacters)
{
	sleep 500 ; time untill you release the hotkey #z
	Length := StrLen(scannedcharacters)

	loop, %Length%
	{
		if a_index <= 0
			continue
		char := SubStr(scannedcharacters, a_index, 1)
		
		IfInString, char, |
		{
			send, (1) ; <<< PUT SOME CODE HERE
			continue
		}
		IfInString, char, ``
		{
			send, (2) ; <<< PUT SOME CODE HERE
			continue
		}
	
		Sendraw, %char%
	}
	return
}
return

#q:: ; DISCOVER YOU ACTIVE TITLE
{
	wingetactivetitle, title
	msgbox, The following title is now on your clipboad:`n%title%
	clipboard := title
}
return

Rafa_WinActivate(Title) ; MY PERSONAL FUNCTIONS
{
	while 1
	{
		rRafa_SetForegroundWindow := Rafa_SetForegroundWindow(Title)
		if(rRafa_SetForegroundWindow == "ERROR_1") ; SCREEN DOESNT EXIST
		{
			return "ERROR"
		}
		else if(rRafa_SetForegroundWindow == "ERROR_2") ; SCREEN NOT ACTIVATED YET
		{
			continue
		}
		else if (rRafa_SetForegroundWindow == "OK") ; SCREEN ACTIVATED
		{
			return "OK"
		}
	}
}
return

Rafa_SetForegroundWindow(Title) ; MY PERSONAL FUNCTIONS
{
	IfWinExist, %Title% 
	{
		WinGet, hWnd, ID, %Title% 
		DllCall("SetForegroundWindow", UInt, hWnd)
		CalledWindow_ID := hWnd
		WinWaitActive
	}
	else
	{
		CalledWindow_ID := "ERROR"
	}
	
	WinGet, ActiveWindow_ID, ID, A
	
	if (CalledWindow_ID == "ERROR")
		return "ERROR_1" ; SCREEN DOESNT EXIST
	
	if (ActiveWindow_ID != CalledWindow_ID)
	{
		return "ERROR_2" ; SCREEN NOT SUCCESSFULLY ACTIVATED 
	}
	else
	{
		return "OK"
	}
}
		

ins:: ; PRESS INSERT TO RELOAD THE SCRIPT 
{
	reload
}
return
794
Posts: 11
Joined: 03 Jun 2016, 15:43

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

06 Jul 2016, 11:34

RAFACN wrote:I made you a code.. look at it. :D

#a hotkey calls the main script (infinite loop.. to exit, write "exit").

For debbugin purposes:
#z hotkey is just for you to test...
#q hotkey is for you to discover you real excel title..
#ins hotkey is for you to reload the script whenever you want

The routine that you have to change is My_Routine().. there you put your {DELETE 10}{ENTER}{...}.

Change "WRITE_YOUR_EXCEL_TITLE_HERE" to your excel real title. if you dont know for sure it, go to your document and press #q.
Wow, I really appreciate the clearly large amount of effort you put into this. However, it isn't really what I was looking for, and not nearly simple enough.

Essentially, this is what I do:

Grab a box and scan the tracking number. The tracking number is the one that I need the first 10 digits off of. Then I type in the location the box was shipped from in another cell, and then scan the serial numbers of the contents of the box in another column of cells. With all that in mind, this script sadly only slows me down. Currently it doesn't take much for me to reach for the keyboard and tap F1 to activate my normal script. I was simply looking for an even quicker way of just scanning a barcode with an activation key so I don't have to reach over.

Thank you again for your hard work.
User avatar
RAFACN
Posts: 12
Joined: 20 May 2016, 10:06
Location: BRAZIL

Re: Script doesn't perform correctly when hotkey is executed via barcode scanner

06 Jul 2016, 12:19

794 wrote:
RAFACN wrote:I made you a code.. look at it. :D

#a hotkey calls the main script (infinite loop.. to exit, write "exit").

For debbugin purposes:
#z hotkey is just for you to test...
#q hotkey is for you to discover you real excel title..
#ins hotkey is for you to reload the script whenever you want

The routine that you have to change is My_Routine().. there you put your {DELETE 10}{ENTER}{...}.

Change "WRITE_YOUR_EXCEL_TITLE_HERE" to your excel real title. if you dont know for sure it, go to your document and press #q.
Wow, I really appreciate the clearly large amount of effort you put into this. However, it isn't really what I was looking for, and not nearly simple enough.

Essentially, this is what I do:

Grab a box and scan the tracking number. The tracking number is the one that I need the first 10 digits off of. Then I type in the location the box was shipped from in another cell, and then scan the serial numbers of the contents of the box in another column of cells. With all that in mind, this script sadly only slows me down. Currently it doesn't take much for me to reach for the keyboard and tap F1 to activate my normal script. I was simply looking for an even quicker way of just scanning a barcode with an activation key so I don't have to reach over.

Thank you again for your hard work.
Ok, friend!
I tought you needed a way to use your barcode scanner to send to excel an interpreted code, not the raw text it scans. But if you say "it doesn't take much for me to reach for the keyboard and tap F1 to activate my normal script" i clearly didnt understand right. =)
btw, it was not much effort for me to script this. i already had all of the functions in another program of mine.. =) just adapted the main function. But thank you for considering that.
Good luck with your script!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 251 guests