OK, I've got it working, and in a rather straightforward manner.
Although dispatchKeyEvent() for some reason does not always determine
which UI element gets focus, it turned out that the UI element
actually does have a focus that one can in onKeyDown() reliably
perform a hasFocus() for after having done setFocusable(),
setFocusableInTouchMode() on all UI elements and a requestFocus(),
requestFocusFromTouch() on one element. Next, after putting all UI
elements like radiobuttons, checkboxes and editboxes in (for instance)
arrays, one can assign focus to the previous or next element when the
volume up or down key is pressed, thus hard-coding in what order UI
elements are traversed. Not very elegant, but thus far it works
reliably (no strange dependencies on whether a view is scrollable),
does the accessibility job for me in all activities, and it did not
require a major program overhaul. Tested on Android 1.6 and 2.2. It is
a bit like defining the tab order when programming for Microsoft
Windows. The key thing was to discard use of dispatchKeyEvent()
altogether and "manually" do what dispatchKeyEvent() was supposed to
do in terms of moving focus to a next UI element for a generated
KeyEvent.KEYCODE_DPAD_DOWN and KeyEvent.KEYCODE_DPAD_UP.

Thanks


On Dec 17, 10:06 am, Dianne Hackborn <[email protected]> wrote:
> I doubt you will get this to work correctly, because you are doing this
> after parts of the framework that deal with things like going in and out of
> touch mode have seen the events.
>
> Why are you doing this?  If a device doesn't have a DPAD, the user isn't
> going to think to use the volume keys to navigate focus(!!).  You should be
> giving them a good touch-based UI.
>
> On Fri, Dec 17, 2010 at 12:38 AM, blindfold <[email protected]>wrote:
>
>
>
> > For Android phones that lack a physical d-pad or trackball (e.g.
> > Alcatel OT-980, Motorola Droid Pro), I am trying to remap the physical
> > up and down volume keys to simulated up and down d-pad keys using code
> > like
>
> > �...@override
> >   public boolean onKeyDown(int keyCode, KeyEvent event) {
> >      if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
> >         KeyEvent ke = new KeyEvent(KeyEvent.ACTION_DOWN,
> > KeyEvent.KEYCODE_DPAD_UP);
> >         dispatchKeyEvent(ke);
> >         return true;
> >      }
> >      return super.onKeyDown(keyCode, event);
> >   }
>
> > Although this works fine with a ListView and with activity views that
> > become scrollable because they are too large to fit on the physical
> > screen, it somehow does not work at all for activity views that remain
> > small enough to fit on the screen. I can test this by increasing the
> > number of radio buttons in some activity view and then see how the
> > above key remapping begins to work fine as soon as there are more
> > controls than fit on the screen. I have tried all sorts of things with
> > setting focus after making things focusable using setFocusable(true),
> > setFocusableInTouchMode(true), requestFocus(), requestFocusFromTouch()
> > and so on, but nothing seems to help. The above onKeyDown() executes
> > in all relevant cases as shown by my Log.i() output, but it is the
> > dispatchKeyEvent() that  in the described cases does nothing. All my
> > activity views are surrounded by <ScrollView></ScrollView> in the XML
> > of their respective layouts. More about the context of this problem at
>
> >http://groups.google.com/group/eyes-free-dev/browse_thread/thread/374...
> > (eyes-free-dev list).
>
> > So the above code works "sometimes", but I need a reliable/general
> > solution. How can one reliably remap physical keys to perform key
> > functions for which there are no physical keys on the Android device?
>
> > Thanks!
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]<android-developers%[email protected]>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Dianne Hackborn
> Android framework engineer
> [email protected]
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to