Page 1 of 1

Conditional Hardware Key Block or Simulate Key Press

Posted: 12 Jun 2019 11:53
by mattd_nf
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.

Re: Conditional Hardware Key Block or Simulate Key Press

Posted: 13 Jun 2019 06:06
by Pepy
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.

Re: Conditional Hardware Key Block or Simulate Key Press

Posted: 15 Jun 2019 16:27
by Desmanto
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().

Re: Conditional Hardware Key Block or Simulate Key Press

Posted: 17 Jun 2019 05:05
by Pepy
@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).

Re: Conditional Hardware Key Block or Simulate Key Press

Posted: 17 Jun 2019 17:10
by Desmanto
@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