android: consume key press, bypassing framework processing

Posted by user360024 on Stack Overflow See other posts from Stack Overflow or by user360024
Published on 2010-06-07T03:34:21Z Indexed on 2010/06/07 3:42 UTC
Read the original article Hit count: 244

Filed under:

What I want android to do: when user presses a single key, have the view respond, but do so without opening a text area and displaying the character associated with the key that was pressed, and without requiring that the Enter key be pressed, and without requiring that the user press Esc to make the text area go away.

For example, when user presses "u" (and doesn't press Enter), that means "undo the last action", so the controller and model immediately undo the last action, then the view does an invalidate() and user sees that their last action has been undone. In other words the "u" key press should be silently processed, such that the only visual result is that user's last action has been undone.

I've implemented OnKeyListener and provided an onKey() method:

the class: public class MyGameView extends View implements OnKeyListener{

in the constructor: //2010jun06, phj: With onKey(), helps let this View consume key presses // before the framework gets a chance to consume the key press. setOnKeyListener((View.OnKeyListener)this);

the onKey() method: public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_R) { Log.d("BWA", "In onKey received keycode associated with R."); } return true; // meaning the event (key press) has been consumed, so // the framework should not handle this event. }

but when user presses "u" key on the emulator keypad, a textarea is opened at the bottom of the screen, the "u" charater is displayed there, and the onKey() method doesn't execute until user presses the Enter key.

Is there a way to make android do what I want? Thanks,

© Stack Overflow or respective owner

Related posts about android