Simulating combination of key presses from ADB ter

2020-06-17 06:49发布

问题:

I want to send "CTRL + W" to the Chrome for Android to close active tab. I tried lots of things but there is no success to achieve it from terminal. (If i connect a USB Keyboard with OTG, i can close the tab with CTRL+W)

Firstly i do not want to write a application for this, i only want a shell command to use it from Tasker.

I read somewhere that to achieve this (CTRL+W keypress), i have to simulate key presses like this:

Down CTRL
Down W
Up W
Up CTRL

And to achieve this from terminal, it seems i have to use "sendevent".

I can simulate all hardware keypress with "sendevent" but can not simulate the normal keys with it.

For example, to down and up to the POWER key:

sendevent /dev/input/event1 1 116 1
sendevent /dev/input/event1 0 0 0
sendevent /dev/input/event1 1 116 0
sendevent /dev/input/event1 0 0 0

i use this commands, but i can not use this commands to send normal keys. (for example a,b,c etc)

The event1 is the gpio-keys, so i'm using it. And all the other input events are sensors and one is the charging driver. (max77693-muic)

The output of the "getevent -p" says that:

add device 1: /dev/input/event9
  name:     "compass_sensor"
  events:
    REL (0002): 0000  0001  0002  0003  0004  0005  0006  0007 
                0008  0009 
  input props:
    <none>
add device 2: /dev/input/event6
  name:     "barometer_sensor"
  events:
    REL (0002): 0000  0001  0002 
  input props:
    <none>
add device 3: /dev/input/event5
  name:     "light_sensor"
  events:
    REL (0002): 0000  0001  0002  0009 
  input props:
    <none>
add device 4: /dev/input/event4
  name:     "proximity_sensor"
  events:
    ABS (0003): 0019  : value 1, min 0, max 1, fuzz 0, flat 0, resolution 0
  input props:
    <none>
add device 5: /dev/input/event3
  name:     "gyro_sensor"
  events:
    REL (0002): 0003  0004  0005 
  input props:
    <none>
could not get driver version for /dev/input/mice, Not a typewriter
add device 6: /dev/input/event7
  name:     "Midas_WM1811 Midas Jack"
  events:
    KEY (0001): 0072  0073  00e2 
    SW  (0005): 0002  0004 
  input props:
    <none>
add device 7: /dev/input/event1
  name:     "gpio-keys"
  events:
    KEY (0001): 0072  0073  0074  00ac 
  input props:
    <none>
add device 8: /dev/input/event0
  name:     "max77693-muic"
  events:
    KEY (0001): 0072  0073  00a3  00a4  00a5 
  input props:
    <none>
add device 9: /dev/input/event8
  name:     "sec_touchkey"
  events:
    KEY (0001): 008b  009e 
    LED (0011): 0008 
  input props:
    <none>
add device 10: /dev/input/event2
  name:     "sec_touchscreen"
  events:
    ABS (0003): 002f  : value 0, min 0, max 9, fuzz 0, flat 0, resolution 0
                0030  : value 0, min 0, max 255, fuzz 0, flat 0, resolution 0
                0031  : value 0, min 0, max 255, fuzz 0, flat 0, resolution 0
                0032  : value 0, min 0, max 30, fuzz 0, flat 0, resolution 0
                0035  : value 0, min 0, max 719, fuzz 0, flat 0, resolution 0
                0036  : value 0, min 0, max 1279, fuzz 0, flat 0, resolution 0
                0039  : value 0, min 0, max 65535, fuzz 0, flat 0, resolution 0
                003c  : value 0, min -90, max 90, fuzz 0, flat 0, resolution 0
                003d  : value 0, min 0, max 1, fuzz 0, flat 0, resolution 0
  input props:
    INPUT_PROP_DIRECT

Also my gpio-keys layout file "/system/usr/keylayout/gpio-keys.kl" like this:

key 115   VOLUME_UP         WAKE
key 114   VOLUME_DOWN       WAKE
key 116   POWER             WAKE
key 172   HOME              WAKE

I can send all normal keyevents with:

"input keyevent KEYCODE_X" 

and to send more than one:

"input keyevent KEYCODE_X KEYCODE_Y"

You should think it can works like this:

"input keyevent KEYCODE_CTRL_LEFT KEYCODE W"

but keyevent down and up immediatly and i can not use it to send CTRL+W combination.

I know, the answer should be with "sendevent". But i can not find.

I also tried to adding some fake keys into the key layout file like this:

key 115   VOLUME_UP         WAKE
key 114   VOLUME_DOWN       WAKE
key 116   POWER             WAKE
key 172   HOME              WAKE
key 19    Q

i restarted the phone, then tried:

sendevent /dev/input/event1 1 19 1
sendevent /dev/input/event1 0 0 0
sendevent /dev/input/event1 1 19 0
sendevent /dev/input/event1 0 0 0

But it never writes "Q" into the any textbox.

Please help, thanks for your helps.

回答1:

Oh YES!! I dont know why but whenever I feel stuck I come to stackoverflow and as soon as I start writing the question somehow I find the answer... xD Anyways, I was able to do it by following procedure:

  1. Go to /system/usr/keylayout/
  2. In my case there was no gpio-keys, anyways open Generic.kl
  3. It has all the keycodes you would need to simulate anything... such as for CTRL_RIGHT keycode is 97 and for W keycode is 17
  4. Thats all you need, now open tasker --> New task --> Add wait 5 sec --> Run shell: input keyevent 97 input keyevent 17

Now run the command and quickly open chrome, voila! in 5 secs you will see your tab disappearing!

Hope that helps all the future tasker pros ;)

Kudos...



回答2:

The events section in getevent -p output lists all accepted key codes:

add device 7: /dev/input/event1
  name:     "gpio-keys"
  events:
    KEY (0001): 0072  0073  0074  00ac 

i.e. VOLUME_UP(0x73), VOLUME_DOWN(0x72), POWER(0x74) and HOME(0xAC) in case of /dev/input/event1. Everything else gets filtered out by the linux kernel input driver long before it reaches the Android framework (where the layout files you tried to modify are used)