12

I am macking a custom navigation bar to Android 4.0.3.r1 and want to send key events like "Home" and "Back". My application is not a system therefore:

IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
                ServiceManager.getService(Context.WINDOW_SERVICE));
mWindowManager.injectKeyEvent( ev, false );

It doesn't work, because I can not get android.permission.INJECT_EVENTS from not system application. How can I do this?

Sergei Vasilenko
  • 2,313
  • 2
  • 27
  • 38

9 Answers9

7
BaseInputConnection  mInputConnection = new BaseInputConnection(targetView, true);
mInputConnection.sendKeyEvent(new KeyEvent(...));
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
Roman Trokhymets
  • 1,102
  • 10
  • 10
7

Here are some precision to Roman answer

BaseInputConnection  mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
Community
  • 1
  • 1
Lotfi
  • 660
  • 1
  • 6
  • 21
4

It works for me:

public static void simulateKey(final int KeyCode) {

    new Thread() {
        @Override
        public void run() {
            try {
                Instrumentation inst = new Instrumentation();
                inst.sendKeyDownUpSync(KeyCode);
            } catch (Exception e) {
                Log.e("Exception when sendKeyDownUpSync", e.toString());
            }
        }

    }.start();
}
iStar
  • 1,112
  • 12
  • 20
  • 1
    Wnen I use these methods it gives me error about INJECT_EVENTS permission. So I follow this post: http://stackoverflow.com/questions/5383401/android-inject-events-permission?rq=1. When I include the permission android.permission.INJECT_EVENTS in manifest file, then it throws an error that permission is granted to system apps. How can I include this permission ? Thanks – user2430771 Sep 16 '14 at 21:38
4

you can try this

try
{
    String keyCommand = "input keyevent " + KeyEvent.KEYCODE_MENU;
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec(keyCommand);
}
catch (IOException e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

of course, you can choose command input text ... for input a text.

Huiyu
  • 77
  • 3
  • Hi, I tried using this to implement my back button but it didn't work. I'm building for Android 4.4.3. I have searched a lot for a week but no success so far. Thanks – user2430771 Sep 16 '14 at 21:45
  • 4
    if you use this method, you need get root previlege – Huiyu Oct 20 '14 at 08:01
  • 2
    This require issue with root as Runtime.getRuntime().exec(new String[] { "su", "-c","input keyevent "+KeyEvent.KEYCODE }); – UdayaLakmal Jun 29 '16 at 06:13
2

None of these are valid. To go to Home screen from use below code.

Intent home = new Intent(Intent.ACTION_MAIN);
home.addCategory(Intent.CATEGORY_HOME);
//home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(home);

If you are not calling from activity/fragment you may have to uncomment the flag part. For going back below code works on some devices.

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

Let me know if this helps!

Ishaan
  • 3,658
  • 2
  • 23
  • 39
  • KEYCODE_BACK is for simulating back button action and to generate click event requires both ACTION_DOWN and then ACTION_UP. – Ishaan Jul 09 '18 at 02:00
2

I'm also having this same problem before, In below way I solve theKEY_INJECT_EVENT_PERMISSION issue.

Step 1: You need to get the Signature (for me the file name is signapk) of your Device ROM.

Step 2: Then you need to get the platform.pk8 and platform.x509.pem files.

Step 3: Generate the the debug apk of your application.

Step 4: Place all the above files in a single folder.

Step 5: Once you get all the above files run the command mentioned in below.

java -jar signapk.jar platform.x509.pem platform.pk8 your_debug_app.apk customname.apk

Step 6: After this you can get a signed apk (customname.apk) in the same folder.Once you get that run the below command.

adb install -r app-release-signed.apk

Step 7: Now the Inject_Event_Permisson will be enabled.

Stephan John
  • 331
  • 1
  • 2
  • 11
0

There is also InputConnection's sendKeyEvent function. InputConnection is only API Level 3.

Protector one
  • 6,926
  • 5
  • 62
  • 86
0

Reviving old thread - You can perform Home and Back with the relatively new Accessibility API - Check out "performGlobalAction" here: http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html

(Specifically with the GLOBAL_ACTION_HOME and GLOBAL_ACTION_BACK actions)

Of course you will need appropriate permissions for an Accessibility Service, but this does not require root

Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
0

You can try this.

long now = SystemClock.uptimeMillis();
BaseInputConnection mInputConnection = new BaseInputConnection(findViewById(R.id.MainActivity), true);
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HOME, 0);
mInputConnection.sendKeyEvent(down);

This code can work for me.

Note : Please remember to replace the "R.id.MainActivity" to your Activity name.