'I'm having issues with the dispatchKeyEvent(), I have a physical scanner and would like to receive the barcode

this works but the problem is that the back press button doesn't work but if I return return super.superDispatchKeyEvent(event) it works but my menu drawer opens when button is clicked

@SuppressLint("RestrictedApi")
override fun dispatchKeyEvent(event: KeyEvent): Boolean {


    if (event.action == KeyEvent.ACTION_DOWN) {
        val pressedKey = event.unicodeChar.toChar()
        barcode.append(pressedKey)

    }
    if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) {

        Toast.makeText(this, "Barcode; $barcode",  Toast.LENGTH_LONG).show()
        barcode.delete(0, barcode.length)

    }

    return false
}

after changing it to the answer below, the toast message doesn't show a barcode

private val barcode = StringBuffer()

@SuppressLint("RestrictedApi")
override fun dispatchKeyEvent(event: KeyEvent): Boolean {

    if (event.action == KeyEvent.ACTION_DOWN) {
        if (event.keyCode == KeyEvent.KEYCODE_ENTER) {

            val pressedKey = event.unicodeChar.toChar()
            barcode.append(pressedKey)

            return true

        } else if (barcode.isNotBlank()){

            Toast.makeText(this, "Barcode; $barcode",  Toast.LENGTH_LONG).show()
            Log.d("scannerBarcoe", "$barcode")
            barcode.delete(0, barcode.length)

            return true

        }
    }
    return super.dispatchKeyEvent(event);
}


Solution 1:[1]

You don't want to do either of those things. This function is supposed to return true if the event is consumed. You're consuming those keystrokes. You should return true in both of those keystrokes, and super.dispatchKeyEvent only if both fail.

Also, your first case is way too broad (it will catch every down event, which isn't what you want), and could screw up the second. You should be doing:

if (event.action == KeyEvent.ACTION_DOWN) {
  if(event.keyCode == KeyEvent.KEYCODE_ENTER) {
    //handle a delete key
    return true

  }
  else if(/*test if this is a legal barcode character*/) {
    //handle  barcode key
    return true

  }
}
return super.dispatchKeyEvent(event);

Solution 2:[2]

I've figured it out with the solution below


  private val barcode = StringBuffer()

  @SuppressLint("RestrictedApi")
    override fun dispatchKeyEvent(event: KeyEvent): Boolean {


        if (event.action == KeyEvent.ACTION_DOWN) {
            
            val pressedKey = event.unicodeChar.toChar()
            barcode.append(pressedKey)

            if (event.keyCode == KeyEvent.KEYCODE_ENTER) {

                Toast.makeText(this, "Barcode; $barcode", Toast.LENGTH_LONG).show()
                Log.d("scannerBarcoe", "$barcode")
                barcode.delete(0, barcode.length)

                return true

            }
        }
        return super.dispatchKeyEvent(event);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabe Sechan
Solution 2 ismail M