Send a keypress event

jscoulter

Member
Licensed User
Longtime User
Hi all.
I am following up on a previous message about answering a call and someone provided some links.
One of the links had the following code:

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent event =new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK); i.putExtra(Intent.EXTRA_KEY_EVENT, event ); context.sendOrderedBroadcast(i, null);

Only problem is, I am not sure how to convert this to B4A...well I can do the Intent part.......but the keyevent stuff I am not sure of.
Anyone able to help?

Jeremy
 

agraham

Expert
Licensed User
Longtime User
From My life with Android :): Generating keypresses programmatically

At this point you may get excited and would try to generate keypresses for other applications. This will not work. Android applications are allowed to generate keypresses only for themselves. In WindowManagerService class, you will find a permission check controlling this behaviour.
...
By default, WindowManagerService does not have this permission, that is why it is impossible to generate events for somebody else's window.
And a quote from Diane Hackborn who is one of the Android development team and who often provides forum support [android-developers] Re: how to inject a KeyEvent - Dianne Hackborn - com.googlegroups.android-developers - MarkMail

Correct, one application can not inject key events into another
application. There should be no way around this.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I know what he's trying to do. He wants to control the media player, and that requires passing a keyevent inside an intent broadcast. That's how widgets do it anyway. But we need that keyevent class
 
Upvote 0
Top