The question of getting started with Android TV: why can't enter events be blocked in onKeyDown?

very novice is getting started, what Android TV, wants to do is to make sure that the confirmation button of the remote control has been pressed. However, it is found that all other keys can enter the onKeyDown event, except that the confirmation key will not enter the onKeyDown event. If there is no way, it can only be intercepted in dispatchKeyEvent.

 override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        Log.d("dispatchKeyEvent", "dispatchKeyEvent(), action=" + event.action + " keycode=" + event.keyCode)
        return super.dispatchKeyEvent(event)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_DPAD_CENTER ->
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show()

            KeyEvent.KEYCODE_DPAD_DOWN ->
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show()

            KeyEvent.KEYCODE_DPAD_LEFT ->
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show()

            KeyEvent.KEYCODE_DPAD_RIGHT ->
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show()

            KeyEvent.KEYCODE_DPAD_UP ->
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show()

            KeyEvent.KEYCODE_BACK ->
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show()
        }
        return super.onKeyDown(keyCode, event)
    }
Mar.11,2021

since only the confirm key cannot capture, set it to default.

Menu