Why are Key-Down-Event and Key-Up-event of a key-press not called on the same widget?


In my Android app I am showing a couple of EditText widgets in table in a matrix order (imagine similar to a spreadsheet). I am attaching key-listeners to these fields to be able to trigger certain actions when the enter or the arrow keys are getting pressed while the cursor in that widget.

This did not work quite as expected. At first I had the impression that some listeners were not called at all, then I noticed they were called but on the wrong widget. I added a log statement and learned that for cursor-keys the key-DOWN event and the corresponding key-UP event were not called on the same widget. I find that extremely counter-intuitive.

The code that attaches these listeners reads like so:

protected void addKeyListener(@NonNull View view, T item, int rowIndex, int columnNr) {
    view.setOnKeyListener((v, keyCode, event) ->
    {
       Log.d(TAG, "OnKeyListener (" + rowIndex + "," + columnNr + "): keyCode: " + keyCode + ", event: " + event);
        ... code handling the keys removed for brevity ...
    }
}

i.e. the first line of the listener is a log statement which gives me e.g. this output:

12:30:16.561 Table_1 D OnKeyListener (1,0): keyCode: 20, event: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_DOWN, scanCode=108, metaState=0, flags=0x8, repeatCount=0, eventTime=99512393527000, downTime=99512393527000, deviceId=0, source=0x301, displayId=-1 } 12:30:16.585 Table_1 D OnKeyListener (2,1): keyCode: 20, event: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_DOWN, scanCode=108, metaState=0, flags=0x8, repeatCount=0, eventTime=99512394001000, downTime=99512393527000, deviceId=0, source=0x301, displayId=-1 }

i.e. the key-down event triggered by pressing the cursor down-key is triggered on the the cell 0 in line 1 (first cell in the second line in my table), but the key-up event is triggered not on the same widget, but the widget one line down and one column to the right.
I would have expected that both events would get called on the same widget.

I could have understood if it would get called on the widget exactly one row down (since that is what a cursor is supposed to do: move focus down one line) and it could be that the cursor-handling is already executed on the down-event and not awaiting the up-event.

But why is it additionally moved one column to the right?
In my application this has the odd looking effect that pressing "down" several times makes the cursor go down in a staircase-like movement from top left to bottom right.

Is that working as designed? Can one avoid that behavior and get both events on the originating widget?

1
Oct 1 at 11:07 AM
User Avatarmmo
#android

No answer found for this question yet.