Understanding ADB Shell Input Events

Understanding ADB Shell Input Events

22 April 2024 Stephan Petzl Leave a comment Tech-Help

Introduction to ADB Shell Commands

When interacting with an Android device via the Android Debug Bridge (ADB), you have the option to use various shell commands to simulate user input. Two common commands for this purpose are adb shell input keyevent and adb shell sendevent. In this article, we’ll explore the differences between these commands and guide you on when to use each for inputting characters.

adb shell input keyevent

The adb shell input keyevent command allows you to send either an event code or a string to the device. This command is high-level and easier to use as it handles the input through the framework layer. The syntax for this command is as follows:

        
          input [text|keyevent]
          input text <string>
          input keyevent <event_code>
        
      

Here are some examples of event codes and their corresponding key actions:

  • 3 –> “KEYCODE_HOME”
  • 4 –> “KEYCODE_BACK”
  • 29 –> “KEYCODE_A”
  • 66 –> “KEYCODE_ENTER”
  • 67 –> “KEYCODE_DEL”

For a complete list of event codes, you can refer to the Android KeyEvent documentation.

adb shell sendevent

On the other hand, adb shell sendevent is a lower-level command that sends input events directly to the Linux input subsystem. This command is more complex and requires detailed knowledge of the Linux input event structure. It is generally used for simulating hardware events or for more fine-grained control over the input.

Choosing Between KeyEvent and SendEvent

For most use cases, especially when inputting characters, adb shell input keyevent is preferred due to its simplicity. It also provides a layer of abstraction, which makes it easier to use and understand. However, if you need to simulate more complex interactions or specific hardware events, adb shell sendevent might be the appropriate choice.

Practical Application with Repeato

If you’re involved in automating tests for your Android apps, you might find yourself frequently using ADB commands to simulate user inputs. This is where Repeato, a no-code test automation tool, can significantly streamline your workflow. Repeato allows you to create, run, and maintain automated tests using a visual approach based on computer vision and AI. It supports all sorts of app frameworks and even comes with ADB on board, enabling you to execute ADB commands through script steps.

With Repeato, you can bypass the complexity of ADB commands for standard input tasks, and instead focus on designing robust automated tests that are fast to edit and run. This not only saves time but also enhances the reliability of your tests.

Like this article? there’s more where that came from!