我最近购买了 Metrologic 条码扫描仪(USB 端口),因为每个人都知道它可以作为开箱即用的键盘模拟器。
如何配置扫描仪和我的应用程序,以便我的应用程序可以直接处理条形码数据?也就是说,我不希望用户专注于“文本字段”,然后在 KeyPress 事件触发时处理数据。
通常条形码扫描仪可以配置为在字符串之前和之后发送一些字符。因此,如果您在条形码字符串之前附加例如“F12”,您可以捕获它并将焦点移动到正确的字段。
检查条形码扫描仪手册如何做到这一点。
虽然您的条形码有一个 USB 连接器。它可以编程为键盘楔形或 RS232。请参阅此页面 http://www.instrumentsandequipmentco.com/support/faq-metrologic.htm 它说
问:USB 键盘和 USB 销售点有什么区别? 当 MX009 设置为作为 USB 键盘进行通信时,扫描的数据将出现在您 PC 上当前处于活动状态的应用程序中。就像在键盘上按下键一样输入数据。当 MX009 设置为作为 USB 销售点设备进行通信时,数据会像 RS232 数据一样传输到 USB 端口,并且 USB 端口必须像 COM 端口一样配置。MX009 出厂设置为 USB 键盘或 USB 销售点。
当您的程序接受 RS232 时,您不再需要将焦点放在文本字段中。
查找回车以了解您的代码何时可以使用完整的条形码。
我猜想最简单的方法是拦截更高级别的按键,例如winforms中的PreviewKeyDownKeyDown
(或在表单上使用,设置KeyPreview
为true
,并用于e.SuppressKeyPress
停止向下传递到控件的键)。设备可能有直接的 API;可能没有。
您可以在表单上使用 OnShortcut 事件来拦截键盘按下。检查您在barcodescanner上配置的前缀是否出现,并设置为Handled al keypresses,直到您获得条形码扫描仪后缀。在您的快捷方式处理程序中构建条形码字符串
以下代码改编自我自己使用的代码,但未经测试以目前的形式。
// Variables defined on Form object
GettingBarcode : boolean;
CurrentBarcode : string;
TypedInShiftState: integer; // 16=shift, 17=ctrl, 18=alt
procedure Form1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
var
Character:Char;
begin
Character:=Chr(MapVirtualKey(Msg.CharCode,MAPVK_VK_TO_CHAR));
if GettingBarcode then
begin
// Take care of case
if (TypedInShiftState<>16) and CharInSet(Character,['A'..'Z']) then
Character:=Chr(Ord(Character)+32);
TypedInShiftState:=0;
// Tab and Enter programmed as suffix on barcode scanner
if CharInSet(Character,[#9, #13]) then
begin
// Do something with your barcode string
try
HandleBarcode(CurrentBarcode);
finally
CurrentBarcode:='';
Handled:=true;
GettingBarcode:=False;
end;
end
else if CharInSet(Character,[#0..#31]) then
begin
TypedInShiftState:=Msg.CharCode;
Handled:=True;
end
else begin
CurrentBarcode:=CurrentBarcode+Character;
Handled:=true;
end;
end
else begin
if Character=#0 then
begin
TypedInShiftState:=Msg.CharCode;
end
else if (TypedInShiftState=18) and (Character='A') then
begin
GettingBarcode:=True;
CurrentBarcode:='';
Handled:=true;
end;
end;
end;