windows 如何在不让用户先聚焦文本框的情况下读取条形码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2688493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:19:23  来源:igfitidea点击:

How can I read barcodes without having the user focus a text box first?

c#javawindowsdelphibarcode

提问by Atlas

I recently acquired a Metrologic Barcode scanner (USB port), as everyone already knows it works as a keyboard emulator out of the box.

我最近购买了一台 Metrologic 条码扫描仪(USB 端口),因为每个人都知道它可以作为开箱即用的键盘模拟器使用。

How do I configure the scanner and my application so that my app can process the barcode data directly? That is, I don't want the user to focus on a "Text field" and then process the data when the KeyPress event fires.

如何配置扫描仪和我的应用程序,以便我的应用程序可以直接处理条码数据?也就是说,我不希望用户专注于“文本字段”,然后在 KeyPress 事件触发时处理数据。

采纳答案by Harriv

Usually barcode scanners can be configured to send some characters before and after the string. So if you append eg "F12" before the barcode string, you can capture that and move the focus to the right field.

通常条码扫描器可以配置为在字符串前后发送一些字符。因此,如果您在条形码字符串之前附加例如“F12”,您可以捕获它并将焦点移动到正确的字段。

Check the barcode scanner manual how to do that.

检查条形码扫描仪手册如何做到这一点。

回答by ibandyop

Although your barcode has a USB connector. It can be programmed as a Keyboard wedge or RS232. See this page http://www.instrumentsandequipmentco.com/support/faq-metrologic.htmWhere it says

虽然您的条形码有一个 USB 连接器。它可以被编程为键盘楔形或 RS232。请参阅此页面 http://www.instrumentsandequipmentco.com/support/faq-metrologic.htm它说的地方

Q. What is the difference between USB Keyboard and USB Point-of-Sale?When the MX009 is set-up to communicate as a USB Keyboard, the scanned data will appear in the current application that is active on your PC. The data is entered just as if the keys were pressed on the keyboard. When the MX009 is set-up to communicate as a USB Point-of-Sale device, the data is transmitted to the USB port like RS232 data and the USB port must be configured like a COM port. The MX009 leaves the factory set for either USB Keyboard or USB Point-of-Sale.

问:USB 键盘和 USB 销售点有什么区别?当 MX009 设置为作为 USB 键盘进行通信时,扫描的数据将出现在您 PC 上的当前活动应用程序中。输入数据就像按下键盘上的键一样。当 MX009 设置为作为 USB 销售点设备进行通信时,数据将像 RS232 数据一样传输到 USB 端口,并且 USB 端口必须像 COM 端口一样进行配置。MX009 出厂时设置为 USB 键盘或 USB 销售点。

When your program accepts RS232 you no longer need focus in a text field.

当您的程序接受 RS232 时,您不再需要将焦点放在文本字段中。

  1. Reprogram your barcode as Point-of-Sale (RS232)
  2. Reprogram to send a suffix usually - carriage-return/CR/$0D at the end of the barcode.
  1. 将您的条形码重新编程为销售点 (RS232)
  2. 重新编程以通常发送后缀 - 在条形码末尾回车/CR/$0D。

Look for the Carriage return to know when the complete barcode is available to your code.

查找回车以了解完整的条形码何时可用于您的代码。

回答by Marc Gravell

I would guess the easiest way to do this would be to intercept key-presses at a higher level, such as PreviewKeyDownin winforms (or use KeyDownon the form, set KeyPreviewto true, and use e.SuppressKeyPressto stop the key going down to the controls). There mightbe a direct API to the device; there might not.

我猜想做到这一点的最简单方法是在更高级别拦截按键,例如winforms 中的PreviewKeyDown(或KeyDown在表单上使用,设置KeyPreviewtrue,并用于e.SuppressKeyPress阻止键向下到控件)。设备可能有直接的 API;可能没有。

回答by Jan Oosting

You can use the OnShortcut event on a form to intercept keyboard presses. Check if the prefix you configured on the barcodescanner appears, and set as Handled al keypresses until you get the barcode scanner suffix. Within your Shortcut handler build up the barcode string

您可以在表单上使用 OnShortcut 事件来拦截键盘按下。检查您在条形码扫描仪上配置的前缀是否出现,并设置为 Handled al keypresses,直到您获得条形码扫描仪后缀。在您的快捷方式处理程序中建立条形码字符串

The following code is adapted from something I use myself, but is untested in its current form.

下面的代码改编自我自己使用的东西,但目前的形式未经测试。

    // 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;