Skip to content

Android 13 [preview] Inject input event not working #3186

Closed

Description

@DerekWuYD
  • I have read the FAQ.
  • I have searched in existing issues.

Environment

  • OS: Windows 10 x64
  • scrcpy version: 1.23
  • installation method: Windows release
  • device model: Google Pixel 6
  • Android version: Android 13 preview

Describe the bug
Inject input event is failed in Android 13, both failed in Mouse event and keyboard event.

adb logcat :

04-14 13:13:10.557  1478  1685 W InputDispatcher: Dropping injected event: Injected event targeted at uid 0 would be dispatched to window '380b0b2 NotificationShade' owned by uid 10225.
04-14 13:13:21.929  1478  1685 W InputDispatcher: Dropping injected touch event: Injected event targeted at uid 0 would be dispatched to window '97ea2a com.google.android.apps.nexuslauncher/com.google.android.apps.nexuslauncher.NexusLauncherActivity' owned by uid 10205.
04-14 13:13:23.640  1478  1685 W InputDispatcher: Dropping injected touch event: Injected event targeted at uid 0 would be dispatched to window '97ea2a com.google.android.apps.nexuslauncher/com.google.android.apps.nexuslauncher.NexusLauncherActivity' owned by uid 10205.
04-14 13:14:00.544  1478  1685 W InputDispatcher: Dropping injected event: Injected event targeted at uid 0 would be dispatched to window '380b0b2 NotificationShade' owned by uid 10225.
04-14 13:14:04.911  1478  1685 W InputDispatcher: Dropping injected touch event: Injected event targeted at uid 0 would be dispatched to window '380b0b2 NotificationShade' owned by uid 10225.
04-14 13:14:04.951  1478  1685 I InputDispatcher: Dropping event because there is no touched window on display 0 to receive it: MotionEvent(deviceId=-1, eventTime=2756762000000, source=TOUCHSCREEN, displayId=0, action=MOVE, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, classification=NONE, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, xCursorPosition=nan, yCursorPosition=nan, pointers=[0: (1100.0, 2226.0)]), policyFlags=0x6b000000
04-14 13:14:04.951  1478  1685 W InputDispatcher: Asynchronous input event injection failed.
04-14 13:14:04.959  1478  1685 I InputDispatcher: Dropping event because there is no touched window on display 0 to receive it: MotionEvent(deviceId=-1, eventTime=2756770000000, source=TOUCHSCREEN, displayId=0, action=MOVE, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, classification=NONE, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, xCursorPosition=nan, yCursorPosition=nan, pointers=[0: (1100.0, 2207.0)]), policyFlags=0x6b000000
04-14 13:14:04.959  1478  1685 W InputDispatcher: Asynchronous input event injection failed.

Activity

  1. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    Oh, that's pretty bad. Thank you for the report.

    Does adb shell input keyevent A (to inject a) work?

  2. DerekWuYD commented on Apr 14, 2022

    @DerekWuYD
    ContributorAuthor

    I try following input command are working.

    $ input tap x y
    $ input swipe x1 y1 x2 y2
    $ input keyevent A
    

    and the ADB toybox version of my device is 0.8.6-android

    Thank

  3. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    That's good news, this means that we could find a solution to make it work (once the sources of Android 13 are published).

  4. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    Asynchronous input event injection failed.

    Could you please test with synchronous event injection:

    diff
    diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java
    index 481c512f..7912eeb1 100644
    --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java
    +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java
    @@ -57,7 +57,7 @@ public class Controller {
         public void control() throws IOException {
             // on start, power on the device
             if (!Device.isScreenOn()) {
    -            device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_ASYNC);
    +            device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_WAIT_FOR_FINISH);
     
                 // dirty hack
                 // After POWER is injected, the device is powered on asynchronously.
    @@ -154,7 +154,7 @@ public class Controller {
                 return false;
             }
             for (KeyEvent event : events) {
    -            if (!device.injectEvent(event, Device.INJECT_MODE_ASYNC)) {
    +            if (!device.injectEvent(event, Device.INJECT_MODE_WAIT_FOR_RESULT)) {
                     return false;
                 }
             }
    @@ -218,7 +218,7 @@ public class Controller {
             MotionEvent event = MotionEvent
                     .obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source,
                             0);
    -        return device.injectEvent(event, Device.INJECT_MODE_ASYNC);
    +        return device.injectEvent(event, Device.INJECT_MODE_WAIT_FOR_FINISH);
         }
     
         private boolean injectScroll(Position position, int hScroll, int vScroll, int buttons) {
    @@ -241,7 +241,7 @@ public class Controller {
             MotionEvent event = MotionEvent
                     .obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0,
                             InputDevice.SOURCE_MOUSE, 0);
    -        return device.injectEvent(event, Device.INJECT_MODE_ASYNC);
    +        return device.injectEvent(event, Device.INJECT_MODE_WAIT_FOR_RESULT);
         }
     
         /**
    @@ -259,7 +259,7 @@ public class Controller {
     
         private boolean pressBackOrTurnScreenOn(int action) {
             if (Device.isScreenOn()) {
    -            return device.injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, Device.INJECT_MODE_ASYNC);
    +            return device.injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, Device.INJECT_MODE_WAIT_FOR_FINISH);
             }
     
             // Screen is off
    @@ -272,7 +272,7 @@ public class Controller {
             if (keepPowerModeOff) {
                 schedulePowerModeOff();
             }
    -        return device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_ASYNC);
    +        return device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, Device.INJECT_MODE_WAIT_FOR_RESULT);
         }
     
         private void getClipboard(int copyKey) {
    @@ -302,7 +302,7 @@ public class Controller {
     
             // On Android >= 7, also press the PASTE key if requested
             if (paste && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && device.supportsInputEvents()) {
    -            device.pressReleaseKeycode(KeyEvent.KEYCODE_PASTE, Device.INJECT_MODE_ASYNC);
    +            device.pressReleaseKeycode(KeyEvent.KEYCODE_PASTE, Device.INJECT_MODE_WAIT_FOR_FINISH);
             }
     
             if (sequence != ControlMessage.SEQUENCE_INVALID) {
    diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.java
    index 763a7fad..57642e30 100644
    --- a/server/src/main/java/com/genymobile/scrcpy/Device.java
    +++ b/server/src/main/java/com/genymobile/scrcpy/Device.java
    @@ -292,7 +292,7 @@ public final class Device {
             if (!isScreenOn()) {
                 return true;
             }
    -        return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC);
    +        return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_WAIT_FOR_RESULT);
         }
     
         /**

    Here is a binary (to replace in your v1.23 release):

    • scrcpy-server sha256:4df13f82362063f59b6294439e9d72dcbc1690e4e6a6c6dd7a74a716981b1d00
  5. DerekWuYD commented on Apr 14, 2022

    @DerekWuYD
    ContributorAuthor

    I try it it's still failed, Input event not working.
    But the power key seem work fine.

    Here is the error response

    PS C:\Users\wuderek\Desktop\scrcpy-win64-v1.23> .\scrcpy.exe
    scrcpy 1.23 <https://github.com/Genymobile/scrcpy>
    C:\Users\wuderek\Desktop\scrcpy-win64-v1.23\scrcpy-server: 1 file pushed, 0 skipped. 107.2 MB/s (99427 bytes in 0.001s)
    [server] INFO: Device: Google - (Android 13)
    INFO: Renderer: direct3d
    INFO: Initial texture: 1440x3120
    [server] ERROR: Could not invoke method
    java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at com.genymobile.scrcpy.wrappers.InputManager.injectInputEvent(InputManager.java:44)
            at com.genymobile.scrcpy.Device.injectEvent(Device.java:195)
            at com.genymobile.scrcpy.Device.injectKeyEvent(Device.java:206)
            at com.genymobile.scrcpy.Device.injectKeyEvent(Device.java:210)
            at com.genymobile.scrcpy.Controller.pressBackOrTurnScreenOn(Controller.java:262)
            at com.genymobile.scrcpy.Controller.handleEvent(Controller.java:106)
            at com.genymobile.scrcpy.Controller.control(Controller.java:73)
            at com.genymobile.scrcpy.Server$3.run(Server.java:133)
            at java.lang.Thread.run(Thread.java:1012)
    Caused by: java.lang.IllegalArgumentException: Targeted input event injection from pid 14218 was not directed at a window owned by uid 0.
            at android.os.Parcel.createExceptionOrNull(Parcel.java:3015)
            at android.os.Parcel.createException(Parcel.java:2995)
            at android.os.Parcel.readException(Parcel.java:2978)
            at android.os.Parcel.readException(Parcel.java:2920)
            at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:1251)
            ... 10 more
    Caused by: android.os.RemoteException: Remote stack trace:
            at com.android.server.input.InputManagerService.injectInputEvent(InputManagerService.java:889)
            at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:595)
            at android.os.Binder.execTransactInternal(Binder.java:1280)
            at android.os.Binder.execTransact(Binder.java:1244)
    

    And I found the InputManager from android code base it seem no big change in adb shell input command.
    https://cs.android.com/android/platform/superproject/+/android-t-preview-2:frameworks/base/services/core/java/com/android/server/input/InputShellCommand.java;l=138;bpv=0;bpt=1

  6. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    Since 9a7d7dbfb55e19e56f932c8820448514837f39c6 (already in Android 12), this code run in the input manager service, so it does not run with shell permissions anymore (like scrcpy does). They probably added more checks to reject such injections from "outside".

    I'm a bit afraid that the only solution will be to call input, so we will be limited to what the shell command input provides, so we could not inject all events (mouse press, mouse release, etc.) as needed 😱

  7. DerekWuYD commented on Apr 14, 2022

    @DerekWuYD
    ContributorAuthor

    Oh! it's bad news, How about get the permission in app_process, is it possiable?
    Or if install an app into devices to obtain input event permission? like vysor did.

  8. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    I think vysor use the same mechanism as scrcpy to inject input events. Could you test it on your Android 13?

  9. DerekWuYD commented on Apr 14, 2022

    @DerekWuYD
    ContributorAuthor

    I found vysor can run well on Android 13. Maybe is a good news, but I can't figure out how it obtain the permission.

    You are right vysor also use app_process, even I force uninstall the vysor app on phone the remote control still running.

  10. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    I found vysor can run well on Android 13. Maybe is a good news,

    Yes, this is good news 👍 Thank you.

    Could you please post the logcat warnings when you try to inject key events with scrcpy? (the logs in the first post seems to refer to motion events only)

  11. rom1v commented on Apr 14, 2022

    @rom1v
    Collaborator

    Could you also test with this change, please?

    diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.java
    index 763a7fad..f6e34f56 100644
    --- a/server/src/main/java/com/genymobile/scrcpy/Device.java
    +++ b/server/src/main/java/com/genymobile/scrcpy/Device.java
    @@ -188,7 +188,7 @@ public final class Device {
                 throw new AssertionError("Could not inject input event if !supportsInputEvents()");
             }
     
    -        if (displayId != 0 && !InputManager.setDisplayId(inputEvent, displayId)) {
    +        if (!InputManager.setDisplayId(inputEvent, displayId)) {
                 return false;
             }
     
    • scrcpy-server sha256:8be19ab6778a5377cf9b2eb5ff61ce025d088794ceb6efd6cf8984c4777b254f
  12. DerekWuYD commented on Apr 14, 2022

    @DerekWuYD
    ContributorAuthor

    I try the new version server file. It's still not working.
    But there no error message in .\scrcpy.exe stdout stream
    I attach logcat here, this log is capture when I run your last version server file.

    logcat.txt

  13. DerekWuYD commented on Apr 14, 2022

    @DerekWuYD
    ContributorAuthor

    I have changed android 13 developer preview 2 ROM build to check this issue, it work good in scrcpy v1.23.
    I feel confused, maybe this issue is cause by early stage weekly build,
    I will keep tracking this issue.
    Thank a lot for your effort.

  14. DerekWuYD commented on Apr 15, 2022

    @DerekWuYD
    ContributorAuthor

    I found that adb monkey is work good in this Android 13 devices with June, 2022 android system secuirty update.
    But it's failed by using scrcpy v1.23

    ADB command

    adb forward tcp:4004 tcp:4004
    adb shell monkey --port 4004
    

    this is the code I tested using C#

    IPEndPoint Ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4004);
    Socket ms = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    ms.Connect(Ip);
    ms.Send(Encoding.ASCII.GetBytes($"tap 1000 2000\n"));
    

    By this method injectEvent is working.

  15. 5 remaining items

  16. rom1v commented on Apr 15, 2022

    @rom1v
    Collaborator

    Awesome! Thank you very much. 🎉

    I'll merge that soon, then publish a new version (1.24).

    I expect many people to report the issue next year, with always the same answer "upgrade to scrcpy >= 1.24" 😄 (similar to #2129 for Android 12)

  17. rom1v commented on Apr 15, 2022

    @rom1v
    Collaborator

    Could you please also test other scrcpy features (shortcuts, copy-paste, --show-touches, etc.)? (to fix them before making a new release if necessary)

  18. DerekWuYD commented on Apr 16, 2022

    @DerekWuYD
    ContributorAuthor

    I have check major function of scrcpy, there look's good.
    Thank you ^^

  19. added 3 commits that reference this issue on Apr 16, 2022
    2d84737
    6733fb7
    7d8b72d
  20. itech-apps commented on Jan 12, 2023

    @itech-apps

    I have another question what if directly use InputManager.getInstance().injectInputEvent()

    Let's try (still by reflection, because getInstance() is not visible):

    diff

    diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java
    index 61168993..3ba4f97a 100644
    --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java
    +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/InputManager.java
    @@ -14,13 +14,13 @@ public final class InputManager {
         public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT = 1;
         public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH = 2;
     
    -    private final IInterface manager;
    +    private final android.hardware.input.InputManager manager;
         private Method injectInputEventMethod;
         private boolean alternativeInjectInputEventMethod;
     
         private static Method setDisplayIdMethod;
     
    -    public InputManager(IInterface manager) {
    +    public InputManager(android.hardware.input.InputManager manager) {
             this.manager = manager;
         }
     
    diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java
    index 6f4b9c04..ea2a0784 100644
    --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java
    +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java
    @@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
     import android.os.IBinder;
     import android.os.IInterface;
     
    +import java.lang.reflect.InvocationTargetException;
     import java.lang.reflect.Method;
     
     @SuppressLint("PrivateApi,DiscouragedPrivateApi")
    @@ -56,7 +57,13 @@ public final class ServiceManager {
     
         public InputManager getInputManager() {
             if (inputManager == null) {
    -            inputManager = new InputManager(getService("input", "android.hardware.input.IInputManager"));
    +            try {
    +                Method getInstanceMethod = android.hardware.input.InputManager.class.getDeclaredMethod("getInstance");
    +                android.hardware.input.InputManager im = (android.hardware.input.InputManager) getInstanceMethod.invoke(null);
    +                inputManager = new InputManager(im);
    +            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
    +                throw new AssertionError(e);
    +            }
             }
             return inputManager;
         }
    • scrcpy-server sha256:7332ec06ef9b11df66e8814ceddd324db0fd2faf0571edcd36b76dd39a1082ce

    because the error message show that can't found injectInputEvent in reflection

    It can find it, but not invoke it:

    Caused by: java.lang.IllegalArgumentException: Targeted input event injection from pid 14218 was not directed at a window owned by uid 0.

    I have another question what if directly use InputManager.getInstance().injectInputEvent()

    Let's try (still by reflection, because getInstance() is not visible):

    diff

    • scrcpy-server sha256:7332ec06ef9b11df66e8814ceddd324db0fd2faf0571edcd36b76dd39a1082ce

    because the error message show that can't found injectInputEvent in reflection

    It can find it, but not invoke it:

    Caused by: java.lang.IllegalArgumentException: Targeted input event injection from pid 14218 was not directed at a window owned by uid 0.

    Hello @rom1v, i have a question about this part of code, when i try run in under Android 13 to injectEvent, i receive this exception : java.lang.SecurityException: Injecting input events requires the caller (or the source of the instrumentation, if any) to have the INJECT_EVENTS permission.

    I'm curious how do you bypass this exception in Android 13 ?

    Thanks in advance for your reply

  21. rom1v commented on Jan 12, 2023

    @rom1v
    Collaborator

    @itech-apps How do you execute the code? Is it from an Android app?

  22. itech-apps commented on Jan 13, 2023

    @itech-apps

    Thanks for your reply @rom1v.

    Yes, i have take the part of InputManager as done (by method reflection ..)

    image

    and then run it on my test app :
    image
    as a result i get this exception in Android 13 (Pixel 6) :
    image

  23. itech-apps commented on Jan 13, 2023

    @itech-apps

    Here is the ROM version : TP1A.220624.021
    [ro.build.characteristics]: [nosdcard]
    [ro.build.date]: [Wed Jul 27 20:42:17 UTC 2022]
    [ro.build.date.utc]: [1658954537]
    [ro.build.description]: [oriole-user 13 TP1A.220624.021 8877034 release-keys]
    [ro.build.display.id]: [TP1A.220624.021]
    [ro.build.expect.baseband]: [g5123b-102852-220720-B-8851166]
    [ro.build.expect.bootloader]: [slider-1.2-8739948]
    [ro.build.fingerprint]: [google/oriole/oriole:13/TP1A.220624.021/8877034:user/release-keys]
    [ro.build.flavor]: [oriole-user]
    [ro.build.host]: [abfarm-release-rbe-64-00076]
    [ro.build.id]: [TP1A.220624.021]
    [ro.build.product]: [oriole]
    [ro.build.system_root_image]: [false]
    [ro.build.tags]: [release-keys]
    [ro.build.type]: [user]
    [ro.build.user]: [android-build]

  24. rom1v commented on Jan 13, 2023

    @rom1v
    Collaborator

    You can't run this from an Android app (you could not get the permission).

    That's the reason why scrcpy is not an Android app, but a java program executed from shell: https://blog.rom1v.com/2018/03/introducing-scrcpy/#run-a-java-main-on-android

  25. itech-apps commented on Jan 13, 2023

    @itech-apps

    Ah, oki thank you @rom1v.
    So i will wait the i response of this reported issue on google about these undocumented behavior : https://issuetracker.google.com/issues/244970399
    (i already posted two comments on it).
    Thanks again for your time and have a good day

  26. added a commit that references this issue on Sep 5, 2025
  27. added a commit that references this issue on Nov 19, 2026
    4fc12b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions