简体   繁体   中英

Capture input from physical laser scanner outside a Xamarin Entry

I'm making a warehouse stock Android app with Xamarin.Forms (XAML + C#) for PDAs with builtin laser barcode scanners.

Workers scan barcodes into an Entry, called ProductBarcode , along with a newline character, in order to trigger the Completed event in that Entry, so it can be captured and handled through an event handler, with a code like this.

private void ProductBarcode_Completed(object sender, EventArgs e)
{
    if (!Product.IsValidBarcode(ProductBarcode.Text))
        DisplayAlert("Error", "Invalid Product Barcode.", "Okay");
    else /* Use of ProductBarcode (irrelevant) */;
}

It works fine. But the problem is that workers often touch the screen (background of the view), and the Entry loses focus. So when they try to scan the next code, they often press the physical scan button (like in EDA50K or similar devices) and the input is not read into the Entry, and it gets lost.

I was wondering if it is possible (or not) to capture the input sent to the background of the ContentPage and send it to the correct Entry. Or even if it is possible to capture the input from wherever in the screen.

I came across this looking for Forms support for android and ios for scanning barcodes. My previous application was a native xamarin android app and I used this to universally capture scanned input:

        public override void HandleDispatchKeyEvent(KeyEvent e) {
        if (!string.IsNullOrWhiteSpace(e.Characters)) {
            HandleInput(e.Characters);
        }
        else if (e.Action == KeyEventActions.Up) {
            if (e.KeyCode == Keycode.Enter) {
                HandleInput(_barcodeChacacters);
                _barcodeChacacters = "";
            }
            else {
                _barcodeChacacters += e.DisplayLabel;
            }
        }
    }

That was inside code for a hidden Fragment I had added to my base view. I know the event in xamarin forms droid is named slightly differently, but I'm assuming it works the same. I haven't tried it because I need a solution that works for Android and iOS. But since you're just targeting android, hope that helps, if you still need a solution after all this time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM