views:

32

answers:

2

How handle all input key an touch event incoming to my Android application?

Is any one place where I can catch all this events?

+1  A: 

Check these out:

http://developer.android.com/reference/android/view/View.OnTouchListener.html

http://developer.android.com/reference/android/view/View.OnKeyListener.html

Key
I need handle not event incomming to current view, but all eventwhich are sent to application by system.
I'm not sure I understand. From what I know, key and touch events are sent to views and dialogs. Could you explain more in detail what you are trying to achieve with your application?
Key
@codespy: Use what @Key mentioned in an abstract class that extends `Activity` and make all your Activities extend from that one.
Macarse
A: 

Just add the following to your initial Activity:

// generic Key Listener
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    Log.d("Activity", "Key pressed"+keyCode);

    switch (keyCode) 
    {
        case KeyEvent.KEYCODE_BACK:
            Log.d("Activity", "Back Key pressed");
        return true;

        case KeyEvent.KEYCODE_MENU:
            Log.d("Activity", "Menu Key pressed");
        return true;

        case KeyEvent.KEYCODE_HOME:
            Log.d("Activity", "Home Key pressed"); // doesn't Print!
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Note, you CAN'T capture the Home key!

Lemonsanver