Post Reply Subscribe to Thread Email Thread

[Q] KeyEvent Trigger not working

17th March 2014, 07:15 PM |#1  
aeppacher's Avatar
OP Senior Member
Thanks Meter: 740
635 posts
Join Date:Joined: Apr 2012
In my currentactivity I have been trying to trigger a keyevent using the following 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:

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?
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes