Conditional Hardware Key Block or Simulate Key Press

Post your questions and help other users.

Moderator: Martin

Post Reply
mattd_nf
Posts: 11
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: 36
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 :D
Device Status: OnePlus 5T (Dumpling) running crDroid 5.x, rooted with Magisk.

User avatar
Desmanto
Posts: 1793
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 20190512, Pie 9.0, Rooted.

Post Reply