Throwing/Simulating KeyStrokes programatically

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Throwing/Simulating KeyStrokes programatically

Postby plusminus » Tue Jan 29, 2008 6:13 pm

Throwing/Simulating KeyStrokes programatically


What you learn: You will learn how simulate KeyStrokes/KeyEvents on an Android-Device.
Difficulty: 1.5 of 5 :)

:idea: Questions/Problems: Simply post below...

What it will look like:
Before & After hitting the Button:
Image Image


Description: The IWindowManager provides easy access to a function that simulates KeyEvents. It is called IWindowManager.injectKeyEvent(...). To simulate a complete KeyStroke, we have to simulate a KeyEvent.ACTION_DOWN AND KeyEvent.ACTION_UP on the specific KeyCode.
In this tutorial we will simulate a KeyStroke to the Menu-Button, to make the Menu popup.

And this is how it works:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.simualtekeys;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.DeadObjectException;
  6. import android.os.ServiceManager;
  7. import android.view.IWindowManager;
  8. import android.view.KeyEvent;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12.  
  13. public class SimualteKeyInput extends Activity {
  14.  
  15.         /* The WindowManager capable of injecting keyStrokes. */
  16.         final IWindowManager windowManager = IWindowManager.Stub
  17.                         .asInterface(ServiceManager.getService("window"));
  18.  
  19.         /** Called when the activity is first created. */
  20.         @Override
  21.         public void onCreate(Bundle icicle) {
  22.                 super.onCreate(icicle);
  23.                 setContentView(R.layout.main);
  24.                
  25.                 /* Make the button do the menu-popup. */
  26.                 this.findViewById(R.id.cmd_simulate_key).setOnClickListener(
  27.                                 new OnClickListener() {
  28.                                         @Override
  29.                                         public void onClick(View arg0) {
  30.                                                 /* Start the key-simulation in a thread
  31.                                                  * so we do not block the GUI. */
  32.                                                 new Thread(new Runnable() {
  33.                                                         public void run() {
  34.                                                                 /* Simulate a KeyStroke to the menu-button. */
  35.                                                                 simulateKeystroke(KeyEvent.KEYCODE_SOFT_LEFT);
  36.                                                         }
  37.                                                 }).start(); /* And start the Thread. */
  38.                                         }
  39.                                 });
  40.         }
  41.        
  42.         /** Create a dummy-menu. */
  43.         @Override
  44.         public boolean onCreateOptionsMenu(Menu menu) {
  45.                 boolean supRetVal = super.onCreateOptionsMenu(menu);
  46.                 menu.add(0, 0, "Awesome it works =)");
  47.                 return supRetVal;
  48.         }
  49.  
  50.         /** Wrapper-function taking a KeyCode.
  51.          * A complete KeyStroke is DOWN and UP Action on a key! */
  52.         private void simulateKeystroke(int KeyCode) {
  53.                 doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));
  54.                 doInjectKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));
  55.         }
  56.  
  57.         /** This function actually handles the KeyStroke-Injection. */
  58.         private void doInjectKeyEvent(KeyEvent kEvent) {
  59.                 try {
  60.                         /* Inject the KeyEvent to the Window-Manager. */
  61.                         windowManager.injectKeyEvent(kEvent.isDown(), kEvent.getKeyCode(),
  62.                                         kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent
  63.                                                         .getEventTime(), true);
  64.                 } catch (DeadObjectException e) {
  65.                         e.printStackTrace();
  66.                 }
  67.         }
  68. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <Button  id="@+id/cmd_simulate_key"
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="Simulate Menu Key Press"
  11.    />
  12. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

That's it :)


If anything is still unclear, feel free to ask :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby cabernet1976 » Wed Jan 30, 2008 2:32 am

Hi plusminus,
You resume tutorials beforehand :D
Cool :!:
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby plusminus » Wed Jan 30, 2008 10:46 am

cabernet1976 wrote:Hi plusminus,
You resume tutorials beforehand :D
Cool :!:


Just some small tuts.
First of four tests was good so I could take a short break :lol:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

can't make this work for WebView

Postby pgmglv » Sun Mar 30, 2008 2:29 am

I inject KeyEvent and try to catch it in javascript with keydown, keyup, keypress event handlers. But I get the event back in onUnhandledKeyEvent. I also tried webview.dispatchKeyEvent with the same result.
Would be grateful for any pointers.
pgmglv
Freshman
Freshman
 
Posts: 2
Joined: Sun Mar 30, 2008 2:21 am
Location: USA

Postby NithinG » Tue May 06, 2008 3:01 pm

Thanks a lot plusminus :D

It helped me a lot..
But i am having some doubts. Insead of this Key event i tried to use mouse (Motion event). So we need to create an instance of that. So Can you give an idea about the arguments which are passing on that.

Thanks and regards,

NithinG
NithinG
Developer
Developer
 
Posts: 36
Joined: Wed Feb 27, 2008 6:24 am

Throwing/Simulating KeyStrokes programatically

Postby renegade » Mon Jun 09, 2008 1:01 pm

Ok, nice work plusminus...
Well, I'm wonder now how to simulate KeyPress event, and send it to some running application.
I try to create on-screen keyboard...

Any ideas?
Regards!!
User avatar
renegade
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 27, 2008 10:39 am
Location: Bury Technologies

Top

Re: Throwing/Simulating KeyStrokes programatically

Postby plusminus » Sun Jun 15, 2008 3:22 pm

renegade wrote:Ok, nice work plusminus...
Well, I'm wonder now how to simulate KeyPress event, and send it to some running application.
I try to create on-screen keyboard...

Any ideas?
Regards!!


Hm sorry, no real idea, as the actual app looses focus :?
Google hast to create a system-wide SW-keyboard or every device will have a HW-Keyboard!

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby saahil » Sun Aug 24, 2008 7:45 pm

hello plusminus,
this is the third tutorial i've taken after i updated my sdk to 0.9beta, and none of the tutorials have worked the way they are supposed to :( . i think its because of the sdk update itself that some functions are depricated. anyway, for this tutorial, i m getting the following errors, plz help me :?:

Code: Select all
this.findViewById(R.id.cmd_simulate_key).setOnClickListener(
R.id cannot be resolved


Code: Select all
windowManager.injectKeyEvent(kEvent.isDown(), kEvent.getKeyCode(),
                         kEvent.getRepeatCount(), kEvent.getDownTime(), kEvent
                                   .getEventTime(), true);

The method injectKeyEvent(KeyEvent, boolean) in the type IWindowManager is not applicable for the arguments (boolean, int, int, long, long, boolean)

The method isDown() from the type KeyEvent is deprecated
Saahil
<think><code></code>.......
saahil
Freshman
Freshman
 
Posts: 5
Joined: Sun Aug 24, 2008 6:42 pm
Location: Manipal, Karnataka, India

New SDK

Postby Txorl » Wed Oct 01, 2008 2:48 pm

Does anybody know if there is a way to Simulating KeyStrokes programatically with the new SDk 1.0 ?
Txorl
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Sep 17, 2008 3:12 pm

Postby My_Name_Is_Neo » Sun Oct 19, 2008 3:54 am

[Bump]
Anyone knows how to do this on new SDK 1.0
My_Name_Is_Neo
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Oct 19, 2008 3:43 am

Postby Bratag » Mon Oct 20, 2008 10:16 pm

I would also like to know that. Why the hell they did away with this I do not know. The more I get into SDK 1.0 the more I hate it.
Bratag
Freshman
Freshman
 
Posts: 5
Joined: Sat Oct 18, 2008 4:28 am

Re: Throwing/Simulating KeyStrokes programatically

Postby amitonrise » Mon Nov 10, 2008 7:23 am

how to inject KeyStroke in a BraoadCAst REceiver
amitonrise
Freshman
Freshman
 
Posts: 3
Joined: Mon Nov 10, 2008 6:58 am

Postby mosquitofs » Mon Jan 12, 2009 4:51 pm

I'm not sure but can it be possible that this class import android.os.ServiceManager; is not avaible anymore on the newest sdk?
mosquitofs
Freshman
Freshman
 
Posts: 5
Joined: Sat Jan 10, 2009 8:20 pm

Postby MrSnowflake » Mon Jan 12, 2009 5:06 pm

amitonrise wrote:how to inject KeyStroke in a BraoadCAst REceiver
You can't and why would you? A BroadcastReceiver is a object which doesn't need user input.

mosquitofs wrote:I'm not sure but can it be possible that this class import android.os.ServiceManager; is not avaible anymore on the newest sdk?
Yup, they removed it some time ago.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby mosquitofs » Mon Jan 12, 2009 5:17 pm

I found a way to start an button on a very easy method... I just Overrided the method onKeyDown and return false

and then did for example this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 KeyEvent kev = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_6);
  2.  
  3.                 onKeyDown(KeyEvent.KEYCODE_6, kev);    
  4.  
  5.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


this would write a 6 ... but my problem is I need to send a KeyEvent which "presses" Endbutton

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 KeyEvent kev = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENDCALL);
  2.  
  3.                 onKeyDown(KeyEvent.KEYCODE_ENDCALL, kev);
  4.  
  5.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

but this is not working because you have no controle of the end button... but I really need this ^^ or do you know which application/method will be started by pressing the ENDCALL Button?
mosquitofs
Freshman
Freshman
 
Posts: 5
Joined: Sat Jan 10, 2009 8:20 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests