Android Question When listview has focus how can you get activity_keypress?

almontgreen

Active Member
Licensed User
Longtime User
When a Listview has the focus, how can you get a hardware keyboard arrow key keypress event (up arrow/down arrow key 19 and 20)? I've read about all the issues with soft keyboard use, etc. But it would seem that since I can see the row highlight change when I press the arrow key on a hardware keyboard that an event has to happen somewhere doesn't it?

It is handy in my case to manipulate a listview with a hardware keyboard arrow keys, hence the question.

Of course, I could use other keys to manipulate the list since other keys besides arrow keys don't get consumed by the listview. But that seems counterintuitive and if someone presses the arrow key inadvertently then my row tracking gets off.

This seems like a stupid question, so I apologize if I've missed something somewhere.
 
Last edited:

panagiotisden2

Active Member
Licensed User
Longtime User
It's possible with activity_keypress it doent matter where in the activity the focus is.

Ps. Do you have a device with up/down arrow buttons? I haven't seen any since the Galaxy 5 (gtI5500) years ago.
 
Upvote 0

almontgreen

Active Member
Licensed User
Longtime User
It's possible with activity_keypress it doent matter where in the activity the focus is.

Ps. Do you have a device with up/down arrow buttons? I haven't seen any since the Galaxy 5 (gtI5500) years ago.

The keyPress gets consumed by the ListView when it has the focus and doesn't trigger Activity_KeyPress for the hardware arrow keys. Other keys are active, just not the arrow keys.

I'm just using an android box with a keyboard attached.
 
Upvote 0

panagiotisden2

Active Member
Licensed User
Longtime User
Can you post the code you use in the activity_keypress to handle the key press?

Also is it triggered when the focus is somewhere else?
 
Upvote 0

almontgreen

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Select KeyCode
        Case 19
            'up arrow
            v_row=v_row-1
            If v_row<0 Then v_row=0
            ListView1.SetSelection(v_row)
            LblText(v_row)
        Case 20
            'down arrow
            v_row=v_row+1
            If v_row > ListView1.Size Then v_row=ListView1.Size
            ListView1.SetSelection(v_row)
            LblText(v_row)
        Case 22    'right arrow
            v_row=v_row+1
            If v_row > ListView1.Size Then v_row=ListView1.Size
            ListView1.SetSelection(v_row)
            LblText(v_row)
        Case 21     'left arrow
            v_row=v_row-1
            If v_row<0 Then v_row=0
            ListView1.SetSelection(v_row)
            LblText(v_row)
    End Select
    Return True
End Sub

In the above case for a single simple listview the left and right arrows hardware keyboard keys don't get consumed but the up/down arrow keys on the hardware keyboard do get consumed by the listview.

NOTE: the code works if the ListView does not have the focus
 
Upvote 0

panagiotisden2

Active Member
Licensed User
Longtime User
Strange. The code should consume the key press like with the keycode back.

I can think of a workaround but in not sure it will work or fit your needs.
Try to set the focus to the activity itself or another view every time the focus gets to the listview.

Sorry I can't help more. Maybe there is a way to do it with reflector or inline java.
 
Upvote 0

almontgreen

Active Member
Licensed User
Longtime User
ListView for a phone or a tablet is much different than for with a hardware keyboard. No need for ListView.getselection because the selection could be out of view on a phone for example. However, it would be a useful function for systems with a keyboard and no touchscreen which is unusual but not for me. The above is a work around to set the selection with the arrow key except that when the listview has the focus the key can't be trapped. When you remove the focus, then the highlighted row is no longer highlighted. I get why nobody cares about this, but still I would find it helpful for my app which uses a hardware keyboard and no touchscreen.
 
Upvote 0

almontgreen

Active Member
Licensed User
Longtime User
One last atemp.. What do you see in the logs if you add log(KeyCode) in the beginning of the sub?
Nothing when the ListView has focus, the sub event never happens for the hardware up or down arrow keys. This doesn't surprise me as I had issues trapping the scroll key for the mouse and had to create an invisible panel on top to capture the event and keep focus from changing. However, in this case I want the ListView to have the focus.

There are at least 3 ways to solve my problem. Either add ListView1.GetSelection as a parameter OR create a ListView1_ChangeSelection event OR have a way to get the keypress event from the ListView when it has the focus.

For the time being, I'm just using other keys to manipulate the list and telling people to not use the up or down arrow keys on the hardware keyboard. Seems a bit silly... ha ha ha. But there you go.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I realize this thread is a bit old, and I'm not entirely on track on what the issue is. However, I did notice something in the code posted, that might (or not) affect things:

Regardless of what the keycode is, it's consumed by the sub. Perhaps it should only be consumed if it matches a case, and return false (=not consumed) under the whole select part.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Can't you use a transparent panel over the list view and if it's a keypress you want to consume, allow it to be consumed, if not then pass to the list view?
 
Upvote 0
Top