Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch keyboard input from barcode scanner

I'm building a basic price checker app that scans a barcode and displays information for the product and am trying to run it on an android-powered tablet that comes with a built-in barcode scanner.

The scanner works and if I put a textbox on the app and focus to it, the barcode I scan gets written onto it just fine - however I have been unable to catch the input without having the app focus on a textbox (the app should have no input areas, only images and textview labels).

The scanner shows up as an HID keyboard on the input android settings.

Almsot all the posts I find here are about using the camera to scan barcodes (built my original prototype using this but performance was subpar). One old post here gave me a hint about overriding the dispatchKeyEvent as so

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getCharacters() != null && !event.getCharacters().isEmpty()) {
        isRunning = true;
        Log.d(TAG, "Starting");

        String barcode = event.getCharacters();
        new myImageTask().execute(barcode);
    }
    return super.dispatchKeyEvent(event);
}

However it doesn't seem to be catching any input.

I looked at overriding KeyUp and KeyDown events but they seem to be explicitly built for catching single key events.

Is there another event I could use to catch and read the scanner's full input or should I just chain the KeyDown event to buffer each individual key into a static variable and, after receiving a special input termination character and run my task on the result?

like image 924
ConnorU Avatar asked Oct 15 '25 09:10

ConnorU


2 Answers

barcodeEditText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                            saveToDBMethod();
                            barcodeEditText.setText("");
                            barcodeEditText.requestFocus();
                        return true;
                    default:
                        break;
                }
            }

            return false;
        }
    });
like image 186
Seun Akinduro Avatar answered Oct 17 '25 01:10

Seun Akinduro


Edit: KeyEvent.getCharacters() was deprecated in Android 29, so I am not sure the below is a viable long-term solution.

I know this was asked a few years ago, but I am able to capture barcode scans from both a Honeywell and Zebra devices, running Android 11 and 10, respectively, by overriding onKeyMultiple in MainActivity:

@Override
  public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
    System.out.println("Key Multiple event: " + event.getCharacters());
    return super.onKeyMultiple(keyCode, repeatCount, event);
  }

Even when I don't have an input in focus, the above fires when I perform a scan. Output:

Key Multiple event: some-barcode

like image 41
stephen.hanson Avatar answered Oct 17 '25 00:10

stephen.hanson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!