sk.baka.ambient.views
Class KeyEventHandler

java.lang.Object
  extended by sk.baka.ambient.views.KeyEventHandler
All Implemented Interfaces:
android.view.KeyEvent.Callback
Direct Known Subclasses:
AbstractKeypadHandler, KeypadManager, MenuHandler

public class KeyEventHandler
extends java.lang.Object
implements android.view.KeyEvent.Callback

Handles key events. onKey(int, int, KeyEvent) is invoked on KeyEvent.Callback.onKeyDown(int, KeyEvent) and KeyEvent.Callback.onKeyMultiple(int, int, KeyEvent). If key-down event is suppressed, the key-up event is suppressed as well.

Optionally, it can be directed to move focus on the Center+Up/Down keys.

Author:
Martin Vysny

Field Summary
protected  java.util.Map<java.lang.Integer,java.lang.Boolean> keyDownHandled
          Map of currently pressed buttons.
 
Constructor Summary
protected KeyEventHandler()
          Creates new handler.
 
Method Summary
static android.view.KeyEvent.Callback compose(android.view.KeyEvent.Callback... keyCallbacks)
          Composes multiple prioritized callbacks.
 boolean isConfirm(int keyCode)
          Checks if given key code is the Center/Enter key.
protected  void keyUp(int keyCode, android.view.KeyEvent event)
          Handles the key-up event.
static boolean moveFocus(android.view.View view, boolean up)
          Moves focus to next view.
protected  boolean onConfirm()
          Invoked on center or KeyEvent.KEYCODE_ENTER key press.
protected  boolean onDown()
          Invoked on down arrow key press.
protected  boolean onKey(int keyCode, int count, android.view.KeyEvent event)
          Invoked on key press.
 boolean onKey(android.view.KeyEvent event)
          Handles a generic key event.
 boolean onKeyDown(int keyCode, android.view.KeyEvent event)
           
 boolean onKeyMultiple(int keyCode, int count, android.view.KeyEvent event)
           
 boolean onKeyUp(int keyCode, android.view.KeyEvent event)
           
protected  boolean onLeft()
          Invoked on left arrow key press.
protected  boolean onRight()
          Invoked on right arrow key press.
protected  boolean onUp()
          Invoked on up arrow key press.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyDownHandled

protected final java.util.Map<java.lang.Integer,java.lang.Boolean> keyDownHandled
Map of currently pressed buttons. If the map contains true then the KeyEvent.Callback.onKeyDown(int, KeyEvent) was handled by this handler and the KeyEvent.Callback.onKeyUp(int, KeyEvent) must be suppressed.

Constructor Detail

KeyEventHandler

protected KeyEventHandler()
Creates new handler.

Method Detail

moveFocus

public static boolean moveFocus(android.view.View view,
                                boolean up)
Moves focus to next view.

Parameters:
view - the current view.
up - moves the focus up if true, moves the focus down otherwise.
Returns:
true if the focus was moved

onKey

protected boolean onKey(int keyCode,
                        int count,
                        android.view.KeyEvent event)
Invoked on key press. By default invokes one of the onUp, onDown... event handlers.

Parameters:
keyCode - the key code
count - the key repeat count. 0 is the first key press
event - the event
Returns:
true if the event was handled and is to be suppressed, false otherwise.

keyUp

protected void keyUp(int keyCode,
                     android.view.KeyEvent event)
Handles the key-up event. The event is not cancelable by overriding class. By default does nothing.

Parameters:
keyCode - the key code
event - the event

onUp

protected boolean onUp()
Invoked on up arrow key press.

Returns:
true if the event was handled and is to be suppressed, false otherwise.

onDown

protected boolean onDown()
Invoked on down arrow key press.

Returns:
true if the event was handled and is to be suppressed, false otherwise.

onLeft

protected boolean onLeft()
Invoked on left arrow key press.

Returns:
true if the event was handled and is to be suppressed, false otherwise.

onRight

protected boolean onRight()
Invoked on right arrow key press.

Returns:
true if the event was handled and is to be suppressed, false otherwise.

onConfirm

protected boolean onConfirm()
Invoked on center or KeyEvent.KEYCODE_ENTER key press.

Returns:
true if the event was handled and is to be suppressed, false otherwise.

onKeyDown

public final boolean onKeyDown(int keyCode,
                               android.view.KeyEvent event)
Specified by:
onKeyDown in interface android.view.KeyEvent.Callback

onKeyMultiple

public final boolean onKeyMultiple(int keyCode,
                                   int count,
                                   android.view.KeyEvent event)
Specified by:
onKeyMultiple in interface android.view.KeyEvent.Callback

isConfirm

public boolean isConfirm(int keyCode)
Checks if given key code is the Center/Enter key.

Parameters:
keyCode - the key code to check.
Returns:
true on Center/Enter key, false otherwise.

onKeyUp

public final boolean onKeyUp(int keyCode,
                             android.view.KeyEvent event)
Specified by:
onKeyUp in interface android.view.KeyEvent.Callback

onKey

public final boolean onKey(android.view.KeyEvent event)
Handles a generic key event.

Parameters:
event - the event to handle
Returns:
true if the event was consumed, false otherwise.

compose

public static android.view.KeyEvent.Callback compose(android.view.KeyEvent.Callback... keyCallbacks)
Composes multiple prioritized callbacks. The callbacks are invoked in order until a callback consumes the key or there are no more callbacks to process.

Parameters:
keyCallbacks - a list of key callbacks. Must not be null nor empty.
Returns:
a callback instance.


Copyright © 2007-2009. All Rights Reserved.