Simulating a keystroke combination from an ADB terminal

I want to send " CTRL + W " to Chrome for Android to close the active tab. I tried a lot of things, but the terminal did not succeed. (If I connect a USB keyboard with OTG, I can close the tab using CTRL + W)

Firstly, I do not want to write an application for this, I want the shell team to use it from the Tasker.

I read somewhere that for this (CTRL + W), I need to simulate keystrokes as follows:

Down CTRL
Down w
Up w
Up CTRL

And to achieve this from the terminal, it seems to me that I should use " sendevent ".

I can simulate all hardware keys with "sendevent", but I can not simulate regular keys.

For example, to go down 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 these commands, but I cannot use these commands to send regular keys. (e.g. a, b, c, etc.)

Event1 are gpio keys, so I use it. And all the other input events are sensors, and one is the charging driver. (Max77693-muic)

The output of "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" looks like this:

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

I can send all the usual key events with:

 "input keyevent KEYCODE_X" 

and send more than one:

 "input keyevent KEYCODE_X KEYCODE_Y" 

You should think that it can work as follows:

 "input keyevent KEYCODE_CTRL_LEFT KEYCODE W" 

but keyevent down and up immediately, and I cannot use it to send a combination of CTRL + W.

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

I also tried adding some fake keys to the key layout file, for example:

 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 he never writes β€œQ” to any text field.

Please help, thanks for the help.

+5
source share
2 answers

OH YEAH! I don’t know why, but whenever I feel stuck, I come to stackoverflow, and as soon as I start writing a question, somehow I find the answer ... xD In any case, I was able to do this by doing the following procedure:

  • Go to / system / usr / keylayout /
  • In my case there were no gpio keys, anyway open Generic.kl
  • It has all the key codes that you need to simulate something ... for example, for CTRL_RIGHT keycode - 97, and for W - key code 17
  • That's 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! after 5 seconds you will see that your tab has disappeared!

I hope that this will help all future specialists;)

Kudo ...

+2
source

The events section of getevent -p output lists all the 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 the case of /dev/input/event1 . Everything else is filtered by the linux kernel input driver long before it reaches the Android framework (where the layout files you tried to change are used)

0
source

Source: https://habr.com/ru/post/1204053/


All Articles