use 2 hardware buttons as trigger

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
tempe222
Posts: 7
Joined: 11 Jan 2019 09:40

use 2 hardware buttons as trigger

Post by tempe222 » 15 Jan 2019 09:08

I want to use 2 hardware buttons e.g. vol down + power pressed simultaneously to start an action. How do I do this?

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

Re: use 2 hardware buttons as trigger

Post by Desmanto » 15 Jan 2019 17:53

In most android, power button can't be intercepted, so use other button. Example, I will use Volume down and volume up key.

You have 2 ways to do it so far. Create the flow with the Trigger Hardware Key Event and select both Volume down and up key; check block event if you need it. We want to press this 2 buttons simultaneously to trigger the action; pressing only one won't run the main flow.

Execution Count
Then the first method is to use Condition execution count. Add it, put count as 2, checked timed and put 500ms (or other shorter value). True, go to your action, example turn on flashlight.
The concept is, when only one key is pressed, the flow triggered, and make the execution count to 1, evaluate to false, action not executed. If nothing triggered anymore during the 500 ms, the execution count is reseted to 0, so any other single press still evaluate to false. If you press that 2 buttons at once, then the flow triggered twice. The first one is evaluated to false, but second on is within the 500 ms and evaluated to true, hence execute the action (turn on flashlight).

The downside of this method is that it doesn't check for the key. You can press 2 buttons, or you can press volume down or volume up only for twice, it still execute the action. Use this only when you don't care about the same button pressed twice.
Combination key.png
Combination key.png (50.33 KiB) Viewed 7564 times
Expression with stop
Second method is stricter and a bit more complex. Still using the same hardware key event as trigger. Change the flow FEP to Stop, at flow editing > 3 dot menu > Options > Stop .... Then create a glovar named global_key, type list. Add 2 value, 0 and 0. It represent the key state, 0 means haven't pressed yet. Then we add condition expression as this

Code: Select all

key = newList("VOLUME_UP", "VOLUME_DOWN");
i = indexOfElement(key, key_code_name);
global_key[i] = 1;

e = false;
if(global_key == newList(1,1))
  e = true
else
  sleep(200);

global_key = newList(0,0);
e
We define the key list in as the Vol up and down. We then track the index in the global_key. Set this index to 1. Means if the key pressed is VOLUME_UP, set the global_key[0] to 1, vol up key has been pressed.

define {e} as false, to state the default logic if nothing pressed after it. Check the glovar, if both key has been set to 1, then change the logic {e} to true. This will continue to execute the main action later. But if not (not all key has been pressed), then sleep for 200ms (you can change the delay). After that reset back the global_key to 0,0 state. check the {e}. Execute main action if both key pressed, and do nothing if any of the key is not pressed.

If you press 2 keys at once during that 200 ms, the flow triggered so fast. The first execute set one of the global_key to 1, and another key stop the first execution, so the line global_key = newList(0,0) is not executed yet before timeout. At the same time, set the other key to 1 too. So if you can't pressed at same time, the flow still triggered, but main action won't be executed.

You can add more keys to pressed at once. Just add the key to the trigger, the key name to the key list, add the global to 0,0,0 and the check to 1,1,1.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply