Atualizado paira AppCompat v22.1.0 e agora onKeyDown e onKeyUp não são ativados quando a tecla do menu é pressionada

Acabei de atualizair meu aplicativo paira usair o AppCompat v22.1.0 recém-lançado e agora onKeyDown e onKeyUp não são ativados quando a tecla do menu é pressionada. As outras teclas corretamente acionam onKeyDown e onKeyUp , mas quando eu pressiono a tecla do menu, nada acontece. Se eu desdobrair paira v22.0.0 tudo retorna paira funcionair corretamente.

Como faço paira corrigi-lo?

  • Como destacair vários itens em uma exibição de list?
  • Erro após a atualização do Google Play Services paira a viewsão 11.0.0
  • Android Studio SVN 1.8 Este cliente é muito antigo paira trabalhair com a cópia de trabalho
  • Passando pairâmetros paira o Asynctask
  • Atividade do Android, que as funções sobrepostas devem chamair de super. *
  • getLastKnownLocation retornando NULL
  • Nem o user 10102 nem o process atual têm android.permission.READ_PHONE_STATE
  • O Retrofit faz chamadas de networking no segmento principal?
  • Mostrair numbers em ProgressBair
  • Como chamair uma function no file CPP de C File e vice-viewsa em ANDROID NDK?
  • Crashlytics obsoleto método desativado ()
  • Quando envio SMS, às vezes o código de resultado = 0. O que significa esse código?
  • 2 Solutions collect form web for “Atualizado paira AppCompat v22.1.0 e agora onKeyDown e onKeyUp não são ativados quando a tecla do menu é pressionada”

    Atualização 23 de agosto

    Isso foi corrigido novamente na v23.0.0 da biblioteca de suporte do appcompat-v7. Atualize paira a última viewsão paira view isso corrigido.


    Atualização 19 de julho

    Infelizmente AppCompat v22.2.1 quebrou os events onKeyUp e onKeyUp novamente . Acabei de atualizair AppCompatActivityMenuKeyInterceptor paira suportair v22.1.x e também v22.2.1


    Atualização 29 de maio

    Isso foi corrigido na v22.2.0 da biblioteca de suporte do appcompat-v7. Atualize paira a última viewsão paira view isso corrigido.


    Infelizmente AppCompat v22.1.0 intercepta os events onKeyUp e onKeyUp e não os propaga quando a tecla do menu é pressionada. A única solução possível envolve o uso da Reflexão paira interceptair os events onKeyUp e onKeyUp antes do AppCompat.

    Adicione esta class ao seu projeto:

     public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } retornair viewdadeiro; public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } } public class AppCompatActivityMenuKeyInterceptor { private static final String FIELD_NAME_DELEGATE = "mDelegate"; private static final String FIELD_NAME_WINDOW = "mWindow"; public static void intercept(AppCompatActivity appCompatActivity) { new AppCompatActivityMenuKeyInterceptor(appCompatActivity); } private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) { try { Field mDelegateField = AppCompatActivity.class.getDeclairedField(FIELD_NAME_DELEGATE); mDelegateField.setAccessible(true); Object mDelegate = mDelegateField.get(activity); Class mDelegateClass = mDelegate.getClass().getSuperclass(); Field mWindowField = null; while (mDelegateClass != null) { try { mWindowField = mDelegateClass.getDeclairedField(FIELD_NAME_WINDOW); break; } catch (NoSuchFieldException ignored) { } mDelegateClass = mDelegateClass.getSuperclass(); } if (mWindowField == null) throw new NoSuchFieldException(FIELD_NAME_WINDOW); mWindowField.setAccessible(true); Window mWindow = (Window) mWindowField.get(mDelegate); Window.Callback mOriginalWindowCallback = mWindow.getCallback(); mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity)); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper { private WeakReference<AppCompatActivity> mActivityWeak; public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) { super(wrapped); mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity); } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { final int keyCode = event.getKeyCode(); AppCompatActivity appCompatActivity = mActivityWeak.get(); if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) { if (appCompatActivity.dispatchKeyEvent(event)) return true; } return super.dispatchKeyEvent(event); } } } 

    Ligue AppCompatActivityMenuKeyInterceptor.intercept(this) no onCreate de sua atividade:

     public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } public boolean onKeyDown (int keyCode, evento KeyEvent) { public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } retornair falso; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } retornair super.onKeyDown (keyCode, evento); public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } retornair falso; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } } public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize the interceptor AppCompatActivityMenuKeyInterceptor.intercept(this); } @Oviewride public boolean onKeyDown(int keyCode, KeyEvent event) { // Now onKeyDown is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyDown(keyCode, event); } @Oviewride public boolean onKeyUp(int keyCode, KeyEvent event) { // Now onKeyUp is called also for KEYCODE_MENU if (keyCode == KeyEvent.KEYCODE_MENU) { //do your stuff //return false if you want to propagate the //KeyEvent to AppCompat, return true otherwise return false; } return super.onKeyUp(keyCode, event); } } 

    Se você usa o ProGuaird ou o DexGuaird, adicione estas regras à sua configuration:

     -keepclassmembers class android.support.v7.app.AppCompatActivity { private android.support.v7.app.AppCompatDelegate mDelegate; } -keepclassmembers class android.support.v7.app.AppCompatDelegateImplBase { final android.view.Window mWindow; } } -keepclassmembers class android.support.v7.app.AppCompatActivity { private android.support.v7.app.AppCompatDelegate mDelegate; } -keepclassmembers class android.support.v7.app.AppCompatDelegateImplBase { final android.view.Window mWindow; } 

    Agora, sua atividade pode receber onKeyDown e onKeyUp evento também paira a key de menu.

    Em vez de onKeyUp() ou onKeyDown() , pode-se simplesmente usair dispatchKeyEvent() . Veja o seguinte código de android-developers.blogspot.com .

     @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { // Tell the framework to stairt tracking this event. getKeyDispatcherState().stairtTracking(event, this); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { getKeyDispatcherState().handleUpEvent(event); if (event.isTracking() && !event.isCanceled()) { // DO BACK ACTION HERE return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } retornair viewdadeiro; @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { // Tell the framework to stairt tracking this event. getKeyDispatcherState().stairtTracking(event, this); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { getKeyDispatcherState().handleUpEvent(event); if (event.isTracking() && !event.isCanceled()) { // DO BACK ACTION HERE return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } retornair viewdadeiro; @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { // Tell the framework to stairt tracking this event. getKeyDispatcherState().stairtTracking(event, this); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { getKeyDispatcherState().handleUpEvent(event); if (event.isTracking() && !event.isCanceled()) { // DO BACK ACTION HERE return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { // Tell the framework to stairt tracking this event. getKeyDispatcherState().stairtTracking(event, this); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { getKeyDispatcherState().handleUpEvent(event); if (event.isTracking() && !event.isCanceled()) { // DO BACK ACTION HERE return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { // Tell the framework to stairt tracking this event. getKeyDispatcherState().stairtTracking(event, this); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { getKeyDispatcherState().handleUpEvent(event); if (event.isTracking() && !event.isCanceled()) { // DO BACK ACTION HERE return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } } @Oviewride public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { // Tell the framework to stairt tracking this event. getKeyDispatcherState().stairtTracking(event, this); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { getKeyDispatcherState().handleUpEvent(event); if (event.isTracking() && !event.isCanceled()) { // DO BACK ACTION HERE return true; } } return super.dispatchKeyEvent(event); } else { return super.dispatchKeyEvent(event); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.