detect tv remote keys andoid studio

Java
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d("debug", "we are here");
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
        case KeyEvent.KEYCODE_DPAD_DOWN:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        case KeyEvent.KEYCODE_DPAD_LEFT:
        case KeyEvent.KEYCODE_BACK:
        case KeyEvent.KEYCODE_ESCAPE:
            Log.d("OnKey", "key pressed!");
            Toast.makeText(MainActivity.this, "key pressed!", Toast.LENGTH_SHORT).show();
            return true;
    }
    return false;
}
Source

Also in Java: