[Q] KeyEvent Trigger not working

Search This thread

aeppacher

Senior Member
Apr 14, 2012
795
777
In my currentactivity I have been trying to trigger a keyevent using the following code:

Code:
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

but when this part of the code runs nothing happens and it doesn't simulate the back button being pressed. Before people recommend finish() or onBackPressed(), they will not work for my required implementation because I need both the home button and the recents button to be triggered too and that apparently can be done by triggering keyevents.

Anyone know whats going wrong here?

EDIT: I added:

Code:
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

to release the key and this worked but it wont work for KeyEvent.KEYCODE_HOME or KeyEvent.KEYCODE_APP_SWITCH. Do I need to declare permissions in my manifest to perform these?