Possible to determine which key was pressed in an If/Then with multiple key conditions?

I'd like to use an If-Then condition like this:

But I need to be able to determine which key was pressed. I know it's not passed in as %TriggerValue or %Trigger%, because these aren't macro triggers ... but is there any other way to find out which key was pressed?

My macro has a section that's the same for both keypresses, so I'd rather not duplicate it. I could split it into a subroutine, of course, but that seems like overkill for a relatively short bit of code. So if I could branch on the actual key pressed within a key pressed if/then, that'd work.

-rob.

I can think of two ways to solve this. I will share one of my two ways here:

image

Of course, you probably won't want to display the character that you pressed, which means you will want to replace the Display action with a Set Variable action that assigns the %ActionResult% token to a variable first, and then you can use that variable in other ways, like testing what it is equal to.

I know what you want, but I don't think you can do that. The closest you can probably get is doing it my way, above. I have another way but it's no better than this way.

Of course, my answers aren't always right. Maybe someone else will have a better answer.

That's a very creative solution—I'll take a look at in detail tomorrow, thanks!

-rob.

Would something like this be suitable?

That is close to my second solution. However he may not like the fact that in your case, if neither M nor A is pressed, it still issues the Alert, which was not in his pseudo-code. You can fix that by placing an IF action around the alert. And then we could remove the nesting if that helps clarity.

I wondered if it might be! Now we know.

Certainly. It was included just for testing and demonstration, but if let us know if you need help removing that, @griffman. :wink:

@peternlewis's advice from the thread 'How to wait for any key in a macro'

  • Enable or activate a hot key triggered macro with the specific keys. This is generally a better way to detect keystrokes anyway, since otherwise tapping the key could be missed, especially if one part of the condition is slow.

Thanks for all the advice. After some experimentation this morning—as I have more keys to trap than just A and M—I chose to add the key trapping to my Javascript, instead of relying on checking for them directly in the macro.

Now my Javascript sets a Keyboard Maestro variable, and then I just check the state of that variable in my loop, and reset it in each conditional test:

            document.addEventListener('DOMContentLoaded', function() {
            
                document.addEventListener('keydown', function(e) {
                
                    e.preventDefault();
                    
                    if (e.code === "KeyM") {
                        window.KeyboardMaestro.SetVariable('local_theKeyPressed', 'M');
                    }
                    if (e.code === "KeyA") {
                        window.KeyboardMaestro.SetVariable('local_theKeyPressed', 'A');
                    }
                    if etc....
                });
            });

I could have done this from the get-go, of course, but I prefer the readability of Keyboard Maestro actions to the (for me) nearly inscrutable code in Javascript. But as I've used the key detection routine before, it wasn't too bad to implement.

Thanks for all the suggestions, which I have saved for future reference, as they will prove useful in other macros, I'm sure!

-rob.

I'm not sure at all what you're trying to do here, Rob @griffman, but I created a way to pick from a Prompt With List using a single keystroke that might apply.

Basically, I created a group that is enabled for a single action and the macro in that group is triggered by a long list of possible keystrokes. The macro then types the given key followed by Return or wrapped in other characters or whatever is necessary to select the desired entry in the PWL.

Maybe you could use that idea in another context than a Prompt With List.

It's a good idea, but I'm working with an onscreen Custom HTML Prompt, and need to respond to keys pressed within that window. JavaScript is the preferred solution, but as it's not my strong suit, I was trying to work around it with conditional actions.

-rob.

Also a better solution. It responds to the keydown event, while a Conditional is only tested at next KM "state check" so could easily miss a quick tap.

1 Like

I don't think that applies to Trigger events, does it?

I gather that it does: see @peternlewis's comment that I linked to above.

No it doesn't. Triggers are enacted by events not by polling.

@peternlewis has mentioned this many times; here's an example:

No, KM triggers are are based on OS events (which is why some oft-requested triggers can't be added -- there's no OS event for them. See @tiffle's link above).

I think that actually says the opposite -- that you should (where possible) use hot key triggers to detect keystrokes as they will always be registered, while they might be missed by a Condition check.

2 Likes

Thank you @Nige_S and @tiffle. I had misunderstood what @August meant (entirely my fault), and was thinking of "triggers" here as keys being held within conditions, rather than official, real Keyboard Maestro triggers! So yes, I am actually with you on this. :wink:

2 Likes