我目前正试图设置一些东西,我可以使用Autohotkey读取条形码并输出文件路径。
我的条形码扫描器读取条形码并将结果作为文本输出(就好像它很快被输入键盘一样),然后输入回车键。
我可以按照自己的喜好格式化条形码,目前我有以下内容:
::**::
input, scan,,{Enter}
If StrLen(scan) <> 11 {
msgbox There was an error - barcode read was not 11 characters long (= %scan% )
} else
{
StringSplit, ScanArray, scan, ~
;msgbox Job ID is %ScanArray1%
;msgbox Part ID is %ScanArray2%
strPathString := "z:\jobs\" . ScanArray1 . "\" . ScanArray2 . ".xxl"
send {home}
send {shift down}
send {end}
send {shift up}
send %strPathString%
send {Enter}
}
return
我使用的条形码格式为:** 12345|67890
。如果扫描此条形码,则计划为:
**
(包括星号后的空格)在开始时激活热弦。12345|67890
读入AHK,strPathString
变为z:\jobs\12345\67890.xxl
现在这种方法正常,但有时候,我遇到的问题是,一旦hotstring触发,似乎花了太长时间才开始阅读输入,导致AHK收到像&#34这样的东西; 345 | 67890&#34;以及缺少前1-4位数的其他结果。如果我的计算机/ CPU忙于其他事情,效果也会更糟。
如果我要让用户按热键(比如ctrl + shift + e)调出一个InputBox,然后读取并处理条形码输入,它每次都能正常工作(我测试了这个),但我和#39; d而是让用户将光标定位在右侧字段中,然后直接扫描条形码而无需使用InputBox。另外值得注意的是,当InputBox处于活动状态时,扫描带有HotString代码的条形码仍处于活动状态,并且&#34; **&#34;在条形码的开头,热弦每次都处理得很好,这让我相信它只是一个AHK&#34;唤醒&#34;时间太慢了。
还有其他方法可以做到更可靠吗?
答案 0 :(得分:2)
使用RegEx powered hotstrings尝试这段代码。
#Include Hotstrings.ahk
SetBatchLines, -1
; Not sure, whether to put "|" or "~" as delimiter here
hotstrings("\*\* (\d{5})\|(\d{5})", "ProcessBarcode")
ProcessBarcode:
msgbox % "Job ID: " $1 "`nPart ID: " $2
return
答案 1 :(得分:0)
做了一些小改动:
Process, priority, , High ; Have the script set itself to high priority - seems to help.
#InstallKeybdHook
#Include C:\Program Files (x86)\AutoHotkey\Extras\Scripts\Hotstrings.ahk
SetBatchLines, -1
hotstrings("!! (\d{5})!(\d{5})", "ProcessBarcode")
Return ; return before the barcode processing starts makes sure that ProcessBarcode doesn't autoexec on script load.
ProcessBarcode:
a := $1
b := $2
strPathString := "z:\Jobs\" . a . "\" . b . ".xxl"
sendmode Input ; This makes the string sending quicker.
send {home}
send {shift down}
send {end}
send {shift up}
send %strPathString%
send {Enter}
return
注意:脚本似乎不喜欢热字符串中的*
,~
等字符,所以我使用过!作为我的键/分隔字符,处理正确。
现在这一切似乎都正常。非常感谢。
答案 2 :(得分:0)
为了完整起见,这是我最终使用的内容。
请注意,我购买了一台新的更可配置的扫描仪(Opticon OPI-3601),我可以将其设置为在每个条形码字符串的开头发送CTRL+D
,似乎只有一个键(或键) -combo)触发脚本立即开始接受输入,或者至少足够快地捕获条形码阅读器的输出。
#InstallKeybdHook
SetBatchLines, -1
^d::
input, scan,,{Enter} ;start accepting input from barcode reader
If (StrLen(scan) <> 11) {
msgbox There was an error - barcode read was not 11 characters long (= %scan% )
} else {
StringSplit, ScanArray, scan, ~ ; split the string into an array
strPathString := "z:\George's Jobs\CHC\Barcode xilog testing\" . ScanArray1 . "\" . ScanArray2 . ".xxl" ; path will need to be changed to final job folder
ControlGetFocus, strCtrlFocus, Select program to run ; check if the "Select program to run" window is in focus.
If (ErrorLevel = 0) { ; if program window is in focus, send keys and path directly
sendmode input ; faster sending
send {home}
send {shift down}
send {end}
send {shift up} ; selects current text in input field
send %strPathString%
send {Enter}
} else {
ControlSetText, Edit1, %strPathString%, Select program to run
ControlSend, Edit1, {Enter}, Select program to run
}
}
return
我不确定键盘挂钩或SetBatchLines
是否仍然有必要,但它们似乎不会受伤。