Conditional Hardware Key Block or Simulate Key Press

Post your questions and help other users.

Moderator: Martin

Post Reply
mattd_nf
Posts: 12
Joined: 21 Dec 2016 15:51

Conditional Hardware Key Block or Simulate Key Press

Post by mattd_nf » 12 Jun 2019 11:53

Does anybody know if it is possible to either conditionally block a Hardware Key Event or simulate the button press? I'd like to be able to use the "Hardware Key Event" trigger to catch the key events, run through some additional logic, and either block the event or allow it to continue depending on certain conditions.

Thanks.

Pepy
Posts: 75
Joined: 31 Oct 2018 10:53
Location: Canada

Re: Conditional Hardware Key Block or Simulate Key Press

Post by Pepy » 13 Jun 2019 06:06

I don't think there's a way to block incoming events, but you can have Automagic block any future events by checking the Block Event option in the Hardware Key Event trigger. You can have this trigger in one flow and have the conditions in another flow that will enable/disable the flow to block the events if the conditions are met.

As for simulating button key presses, you can use the Execute Root Command action with "input keyevent <key code>".
You can find more info about what number each key corresponds to here. Just note that this requires root.
Newbie to device automation! Hope my post was helpful, useful or informative :D
Device: OnePlus 5T (Dumpling) running crDroid 5.x, rooted via Magisk 8-)

User avatar
Desmanto
Posts: 1981
Joined: 21 Jul 2017 17:50

Re: Conditional Hardware Key Block or Simulate Key Press

Post by Desmanto » 15 Jun 2019 16:27

To add up, you can still do it without root. But you have to switch to automagic keyboard first and then use Control UI to send the key.

The idea is, use the hardware key event to block the key, so it is intercepted only by Automagic. Do your script and calculate the logic. If the result is to pass the key to the OS, then switch to Automagic keyboard, use Control UI, sendKey() to send the key and then switch back to default keyboard. You can switch the keyboard without root, using Set System setting. But this introduce a big latency, can be up to 1 seconds. Since you have to switch the keyboard first before using sendKey().
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190829, Pie 9.0, Rooted.

Pepy
Posts: 75
Joined: 31 Oct 2018 10:53
Location: Canada

Re: Conditional Hardware Key Block or Simulate Key Press

Post by Pepy » 17 Jun 2019 05:05

@Desmanto
I like your idea of blocking and letting Automagic handle every hardware key event. However, I don't think Automagic keyboard is necessary since Control UI already has many of the functions for simulating physical/on-screen buttons except for the volume keys I suppose (but you could just use the Store Audio Volumes action, do calculation on the value and then use the Set Audio Volumes action to set the new volume level).
Last edited by Pepy on 22 Jun 2019 21:46, edited 1 time in total.
Newbie to device automation! Hope my post was helpful, useful or informative :D
Device: OnePlus 5T (Dumpling) running crDroid 5.x, rooted via Magisk 8-)

User avatar
Desmanto
Posts: 1981
Joined: 21 Jul 2017 17:50

Re: Conditional Hardware Key Block or Simulate Key Press

Post by Desmanto » 17 Jun 2019 17:10

@Pepy : If we want to press button or access certain UI Element (clickById(), checkById() ), then yes, Automagic keyboard is not needed. But since we want to simulate "key press" (A, B, C, DPAD, Volume button, etc), we must use Automagic keyboard to achieve it. Or it can be done using adb input, but require root. And adb input is very slow, especially when emulating keyboard typing, approx 2-3 keys/second
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190829, Pie 9.0, Rooted.

Post Reply