如何捕获从HID条码扫描器发送到“文本”字段的“所有”字符?

时间:2019-09-25 05:28:29

标签: eclipse swt barcode hid

我需要从条形码扫描仪捕获输入。到目前为止,输入只是我在一个“文本”字段中捕获的简单字母数字文本。我在文本字段中添加了ModifyListener,并能够看到输入内容。效果很好。

我现在需要处理一个更复杂的矩阵代码,其中包含多个字段的值。这些值由不可打印的字符分隔,例如RSGSEOT(0x1E,0x1D,0x04)。完整的数据流具有定义明确的标头和末尾的EOT,因此我希望可以检测到条形码输入,而不是手动输入。

检测到条形码时,我可以使用记录分隔符RS拆分消息,并将值插入相关的“文本”字段中。

但是,“文本”控件上的标准按键处理程序将忽略这些不可打印的字符,并且它们不会出现在控件文本中。这使得无法按计划进行。

如何修改这些文本字段以接受和存储所有字符?还是我可以使用另一种方法?

1 个答案:

答案 0 :(得分:0)

这是我用来处理条形码流的代码。

public class Main
{
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args)
    {
        Display d = new Display();
        Shell shell = new Shell(d);

        shell.setLayout(new FillLayout());

        Text text = new Text(shell, 0);

        text.addListener(SWT.KeyDown, new Listener()
        {
            @Override
            public void handleEvent(Event e)
            {
                // only accept real characters
                if (e.character != 0 && e.keyCode < 0x1000000)
                {
                    sb.append(e.character);

                    String s = sb.toString();
                    // have start and end idents in buffer?
                    int i = s.indexOf("[)>");
                    if (i > -1)
                    {
                        int eot = s.indexOf("\u0004", i);
                        if (eot > -1)
                        {
                            String message = s.substring(i, eot + 1);
                            handleMessageHere(message);

                            // get ready for next message
                            sb = new StringBuilder();
                        }
                    }
                }
            }
        });

        shell.open();
        while (!shell.isDisposed())
        {
            if (!d.readAndDispatch())
                d.sleep();
        }
    }