2

What I'm trying to do

I am trying to open a tab in Chromium using ADB. What I'm trying to accomplish is opening a tab within chromium using their hotkeys. I'm trying to hit this line of code from their source: https://cs.chromium.org/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/KeyboardShortcuts.java?rcl=120f241e3761e263d28f1b9194bd6cbf7aa56efd&l=215

Here's the various things I've tried:

Using Android Key Events: https://developer.android.com/reference/android/view/KeyEvent

adb shell sendevent /dev/input/event0 1 114 1
adb shell sendevent /dev/input/event0 1 48 1
adb shell sendevent /dev/input/event0 1 114 0
adb shell sendevent /dev/input/event0 1 48 0

Using Android key codes from source: https://android.googlesource.com/kernel/msm.git/+/android-msm-hammerhead-3.4-kk-r1/include/linux/input.h

adb shell sendevent /dev/input/event0 1 97 1
adb shell sendevent /dev/input/event0 1 20 1
adb shell sendevent /dev/input/event0 1 97 0
adb shell sendevent /dev/input/event0 1 20 0

Using keyevent inputs

adb shell input keyevent KEYCODE_CTRL_RIGHT
adb shell input keyevent KEYCODE_T

An alternative way (but not what I'm trying to accomplish)

One way I've managed to accomplish opening a tab, but not a way I'm in favor of us using intents. I want to open the tab using the actual key events instead of using this intent.

Way that does open a tab:

adb shell am start -a "android.intent.action.VIEW" -d "google.com"

Other StackOverflow articles I've referenced:

ryandawkins
  • 1,537
  • 5
  • 25
  • 38
  • 1. Are you sure that the right event file for your devce is event0? 2. In your first attempt (key codes 114 and 48) I think you should swap the last two lines - release the 'T' before you release the 'ctrl' key. 3. In your second attempt - you press the 'T' first, and only then the 'ctrl' - fix it. – TDG Aug 24 '19 at 08:01
  • Tried swapping the last two lines of the first attempt. No change :\ I don't understand what you're suggesting with the second attempt. 97 is the keycode for CTRL and 20 is the keycode for T. Also, how do I figure out which event input is correct? – ryandawkins Sep 11 '19 at 20:26
  • @ryandawkins this is late but `adb shell getevent` will show the list of inputs and their names, that can help decide which one to send to – S Raghav Mar 17 '21 at 17:20
  • Unable to edit that comment, for the android emulator, I needed to do `adb shell` and then `getevent` in the prompt, didnt print anything when I ran it directly – S Raghav Mar 17 '21 at 17:27

1 Answers1

0

You have to send 0 0 0 after every event:

adb shell sendevent /dev/input/event0 1 29 1
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 20 1
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 29 0
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 20 0
adb shell sendevent /dev/input/event0 0 0 0

(This uses LEFT_CTRL (29) instead of RIGHT_CTRL (97). Just swap out the numbers if you want to use RIGHT_CTRL. For any other key, check Linux's keyboard input keycodes.)

Here's some convenience functions to use in adb shell:

keydown() { sendevent /dev/input/eventN 1 "$1" 1; sendevent /dev/input/eventN 0 0 0; }
keyup() { sendevent /dev/input/eventN 1 "$1" 0; sendevent /dev/input/eventN 0 0 0; }

Replace eventN with the actual event file, which you can find by running getevent without any args and inspecting the list.

Explanation

The Linux input event queue requires the special EV_SYN event to be sent after bunches of events to process the events. (Quick summary: EV_SYN, the "synchronize" event, seperates temporally distinct sets of events.) This corresponds to event 0 0 0. You can see this if you run getevent and try performing the event manually:

emu64xa:/ # getevent -l /dev/input/event0
EV_KEY       KEY_S                DOWN
EV_SYN       SYN_REPORT           00000000
EV_KEY       KEY_S                UP
EV_SYN       SYN_REPORT           00000000

And getevent without the -l flag displays the numbers you need to use:

emu64xa:/ # getevent /dev/input/event0
0001 001f 00000001
0000 0000 00000000
0001 001f 00000000
0000 0000 00000000
virchau13
  • 1,175
  • 7
  • 19