40febf4a91
Virtual device is only for keyboard sources, not mouse or touchscreen sources. Here is the value of InputDevice.getDevice(-1).toString(): Input Device -1: Virtual Descriptor: ... Generation: 2 Location: built-in Keyboard Type: alphabetic Has Vibrator: false Has mic: false Sources: 0x301 ( keyboard dpad ) InputDevice.getDeviceId() documentation says: > An id of zero indicates that the event didn't come from a physical > device and maps to the default keymap. <https://developer.android.com/reference/android/view/InputEvent#getDeviceId()> However, injecting events with a device id of 0 causes event.getDevice() to be null on the client-side. Commit 26529d377fe8aae2fd1ffa91405aa801a0ce8a57 used -1 as a workaround to avoid a NPE on a specific Android TV device. But this is a bug in the device system, which wrongly assumes that input device may not be null. A similar issue was present in Flutter, but it is now fixed: - <https://github.com/flutter/flutter/issues/30665> - <https://github.com/flutter/engine/pull/7986> On the other hand, using an id of -1 for touchscreen events (which is invalid) causes issues for some apps: <https://github.com/Genymobile/scrcpy/issues/2125#issuecomment-790535792> Therefore, use a device id of 0. An alternative could be to find an existing device matching the source, like "adb shell input" does. See getInputDeviceId(): <https://android.googlesource.com/platform/frameworks/base.git/+/master/cmds/input/src/com/android/commands/input/Input.java> But it seems better to indicate that the event didn't come from a physical device, and it would not solve #962 anyway, because an Android TV has no touchscreen. Refs #962 <https://github.com/Genymobile/scrcpy/issues/962> Fixes #2125 <https://github.com/Genymobile/scrcpy/issues/2125>