Android Question ListView traps Activity_KeyPress event for keys 19 and 20?

almontgreen

Active Member
Licensed User
Longtime User
Is there a way for a ListView to pass ALL KeyPress events from a USB connected hardware keyboard? The Sub Activity_KeyPress event does not fire for the up arrow and down arrow keys (19 and 20) when a ListView has focus.

I tried the inline Java and couldn't figure out how to specify getting the keyCode event from the ListView which is one level below the android.view.InputEvent so the InputEvent I presume happens at the ListView level and gets consumed by the ListView.

I asked this question a different way in a previous thread and have been trying workarounds and got this far with my investigation... :(
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject = Activity
   Dim ev As Object = jo.CreateEvent("android.view.View.OnKeyListener", "key", False)
   jo.RunMethod("setOnKeyListener", Array(ev))
End Sub

Sub key_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Return False
End Sub

Is the event raised when you click on the keyboard keys?
 
Upvote 0

almontgreen

Active Member
Licensed User
Longtime User
No, for the code above the event key_Event does not happen, even when listView does not have focus. I tried a few things and even commented out the Sub Activity_KeyPress and that, along with everything else I tried had no effect. Could there be a typeO in the above code?

It seems interesting that the ListView doesn't pass the two key events for keys 19 and 20 (up and down arrow) yet passes all other keys for my hardware keyboard when using a standard Sub Activity_KeyPress?

When the ListView does not have focus, the keys trigger Activity_KeyPress as expected. It is only when the ListView has the focus. The two keys 19 and 20 (up and down arrow) highlight the text in the ListView. So, everything seems to work as described - it just isn't helpful...:(
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Could there be a typeO in the above code?
There is no typo.

The problem is that ListView intercepts these key strokes internally.

Maybe you shouldn't use ListView at all. Add the items to tall panel and move it yourself.

Another thing that you can try it to play with the listview focusable property:
B4X:
Dim jo As JavaObject = ListView1
jo.RunMethod("setFocusable", Array(False))

You can make it focusable when the user touches the screen and later disable it.
 
Upvote 0

almontgreen

Active Member
Licensed User
Longtime User
:)Thanks. There are several options I was thinking about - like just using other keys. There can be issues changing focus when using libraries that trap the scroll wheel for example. For anyone else thinking about this, I'd recommend using other keys and setting the selection in code. If you absolutely have to use up/down arrow keys then use something besides a ListView.
 
Upvote 0
Top