On Cancel in a Custom HTML Prompt Form, Exit the Macro

Have successfully built macro using Custom HTML and really pleased with this feature.

Currently, when I press cancel in the HTML Form, the macro still runs with default values. I would like to cancel/terminate the macro on pressing cancel in the form. I've re-read the manuals, searched the forums and not been able find a solution.

Thanks in advance.

Congratulations! Not a minor achievement, I know. :slight_smile:

Here's what I do, immediately after running the HTML prompt:

I think the check for "is empty" is probably to check to see if the "Escape" key as used to exit the prompt - I'm not positive.

If you learned anything interesting, please post about it. As you probably know, this is an under-documented feature, mostly because it's HTML which means it can get pretty complex (at least for me).

If you haven't already done so, you might also want to look at this:

1 Like

Perfect. Got this working (or something like it) that passes my testing.

This makes sense. In JavaScript, "pressing a button" action is separate from "press escape key" so handling both cases is likely necessary.

1 Like

In KM 8+, you can use the Assert Action for this type of problem.

(I know this is an old topic, but I ran across this problem yesterday and used Dan's solution, then found Peter's post on the Assert action today and it's another great solution.)

1 Like

Similarly, how I can use the Return key to simulate submitting?

Came across this and saw the last question was not answered. I'll give my answer here in case people will search for it.

First, in the HTML JS code, we put an event listener function here:

    function KMInit() {
        window.addEventListener("keydown", function (e) {
            if (e.keyCode == 13) {
                window.KeyboardMaestro.Submit();
            }
        });
    }

Here, keyCode 13 === the "Return" key.

When the "Return" key is pressed, the HTML prompt window will be submitted, and the token %HTMLResult% will be an OK string. We can use this in the following condition action.

If we use @cfriend's approach, we don't need the "OK" string. Mine looks like this:
image

The %HTMLResult% token (v8+) is set to the the parameter in the Submit() function, and can be used in actions later in this instance like any other text token. The deprecated HTML Result Button variable is also set to the parameter.

See: action:Custom HTML Prompt [Keyboard Maestro Wiki]

1 Like