我最近购买了一台Metrologic 条形码扫描仪(USB接口),众所周知它可以直接作为键盘模拟器使用。
那么,我该如何配置扫描仪和我的应用程序,使得我的应用程序可以直接处理条形码数据,而无需用户聚焦在“文本字段”上,并等待呼出 KeyPress 事件后再处理数据?
问:USB键盘和USB销售点之间有什么区别? 当MX009设置为通信为USB键盘时,扫描的数据将出现在您PC上正在运行的当前应用程序中。该数据输入方式就像按下键盘上的按键一样。当MX009设置为通信为USB销售点设备时,数据会像RS232数据一样传输到USB端口,并且必须将USB端口配置为COM端口。MX009出厂时已经设置为USB键盘或USB销售点。
当您的程序接受RS232时,您不再需要在文本字段中进行聚焦。
查找回车符以了解完整的条形码何时可供您的代码使用。
KeyDown
,将KeyPreview
设置为true
,并使用e.SuppressKeyPress
阻止键传递到控件)。可能会有直接针对设备的API,也可能没有。 // 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;