How to capture app switch key using onKeyDown in Android?

Abid picture Abid · Dec 20, 2011 · Viewed 7k times · Source

I am trying to capture app switch key and home key on android 3.1 and 4.0 but it doesn't seem like its working.

here is what I am doing

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
   if (KeyCode == KeyEvent.KEYCODE.KEYCODE_APP_SWITCH && event.getRepeatCount() == 0)
    { 
        Log.d ("onkeydown","app switch key");
    }
   else if (KeyCode == KeyEvent.KEYCODE.KEYCODE_HOME && event.getRepeatCount() == 0)
    { 
        Log.d ("onkeydown","home key");
    }
    //EDIT:
    return super.onKeyDown(keyCode, event);
}

My log.d statement is not printed. Is it possible to capture these 2 keys?

updated code with correct return statement

Answer

BRPocock picture BRPocock · Dec 20, 2011

Um.

Well, no, you can't.

public static final int KEYCODE_HOME

Since: API Level 1

Key code constant: Home key.

This key is handled by the framework and is never delivered to applications.

http://developer.android.com/reference/android/view/KeyEvent.html