Is there a way to have a Prompt for User Input that accepts only a single key character, and automatically submits the Prompt input when that key is pressed?
I'm trying to build a macro that clicks on a GUI dropdown menu to expose its contents, and want to be able to just press a single numeric key to select the "x"th item visible in the dropdown list, without having to click "OK" or press "Enter" on the Keyboard Maestro User Input popup field.
What you probably need is a different action called Custom HTML Prompt, which probably does support that. I'm not too adept with HTML, but I'll do a little googling to see if I can get that working for you.
Yes, this is possible if one includes some Javascript in the HTML code. I presume you want the action to return the number of the item that the user clicked on. And I presume you want the action to receive and then display a list of items. What is the maximum number of items you want the action to support? Since you want a single digit to be a selection key, then I presume you are limited to 9 items, right? And what is the maximum width of the items in your list? Do you want long items to be wrapped around into secondary lines? Do you want numbers displayed with each item, or will you be handling that? Do you want the window to be moveable? Do you want the window to be resizable? Do you want the window to have a frame? Do you want the window's text to be white on black, or black on white, or colour-selectable?
I might be able to code this for you, but I'm going to need to ask ChatGPT about this because I've rarely touched Javascript. EDIT: I seem to have the basics working.
It only supports the Friendly Value - Syntax like A__Choice 1|B__Choice 2|C__Choice 3 where the Single Letter will never leave the Result of the prompt unless you’re Canceling it or something like the default Value |%Variable%Choice 3%.
Maybe if you use the Friendly Value Syntax & distinguish what was predefined for your Values which pass the Prompt into the Macro there is a chance to do it but everything else (despite mixing both ways) needs custom Code from you.
Then run the first macro, and it will let you select one of nine items with a single key from 1 to 9. That value is passed to the second macro, at which time it is displayed.
So yes, it is possible. My solution is merely to show that it is possible. It's not pretty.
Okay, and I am happy to report that I now have an actual working macro that handles real-world data in a useful and pleasant way. First I will show the actual code, followed by an explanation and example.
When you call this macro, with a few lines of data, you will get a window that looks like this: (pardon the darkness, but I had to take a photo with my camera, because the Javascript that I wrote seems to disable ALL keys, so I couldn't even take a screen snapshot! And pardon the spelling error, please.)
In order to create that window you can run code like the following. This example shows how to pass multiple lines to this macro, which is a subroutine.
The result of this macro is stored into a global variable called "JavaResult". That's where your "keystroke" is returned. If the keystroke is something other than 1 to 9, the value returned is -1, which you can interpret as an error.
One of the tricks that I used to make this solution work as a SINGLE macro is that I had the macro's javascript call itself with the result, and the macro is able to detect the difference between a user calling it and when it calls itself. This simplifies things for the user, but made it more complex for me to write this macro.
Obviously there are many different approaches that could solve this. If anyone wants to offer a different solution, that's great. It was fun writing this, and learning things. Hopefully I didn't make any mistakes. If you find a mistake, I'll fix it.
set theValue to ""
tell application "System Events"
tell process "Keyboard Maestro Engine"
repeat
if (exists window 1) and title of window 1 contains "User Input" then exit repeat
delay 0.1
end repeat
tell window 1
repeat until value of text field 1 is not ""
delay 0.1
end repeat
click button "OK"
end tell
end tell
end tell
As always with my nonsense it could stand improvement -- handling if the dialog is cancelled, for example.
Create a Macro Group
Create Macros for each desired hot key, with appropriate name and trigger as desired, and action Set Variable "Result" to "<trigger>".
Configure the macro group to be Show a palette for one action when, and leave the rest of the configuration unset.
In your macro, set the variable "Result" to empty string, and then use the Show Macro Group for One Action action to show your group, and then WhileVariable "Result" is empty do nothing.
And it makes me wonder if it's possible to solve this using a similar method but using a macro with a typed text trigger with the wildcard of "[0-9]" or just ".".
(Anyone happen to build a test macro accordingly? e.g. set variable to trigger, show a palette for one action ... sounds like maybe its nifty enough to go on a "Alfred Gallery"-esque site, don't think we have an exact equivalent)
--
Nice jumping on that!
In case anyone would like to see, I was exposed to, & used "free" tools to iterate on, one similar custom HTML prompt method here: Literary Toolbox IV and Text Toolbox II