Key Commands in a Dictionary?

A bit more of the context and purpose might help to make the question slightly more easily intelligible.

Dictionary means KM dictionary ? (rather than, for example, JS or PY)

How are you going to use the key (strokes|commands|names|codes|???) ?


If you were going to make some scripted use of them, it might be the codes that you wanted. For other uses it might be printable strings, or names, or their bindings to something else, like KM macros.

1 Like

Hello, yes, the link you sent me is part of the solution, thank you. A KM dictionary. Instead of manually specifying key commands in KM, key commands would be stored in a dictionary. To find the specific key command in a given situation, the dictionary's key would be referenced, with the key command loaded into a variable. The variable would then be used to execute it's self-contained key command.

I supposed key commands could be encoded with a delimiter for combination keys (e.g. command, option). This string could then be passed to AppleScript to "type" the command. It'd be nice to stay inside KM, tho. Am I missing a way to do that?

Than you, Kindly.

What does the term "command" mean exactly here ? The code of a keystroke in a form that AppleScript can use ?

One problem here is that the code for particular letters varies with the keyboard layout.

(The link above give as set of correspondences that are typical on US keyboards, may not match other layouts)

1 Like

Thank you, again. By command, I mean key commands…the terminology overlaps.

Do you have an example of:
1 what you are trying to do, and
2 in what app

it can be in pseudo-code as in:

When I press Ctrl-F, I want to
Open Notes app
Press Cmd-G
etc
etc
1 Like

Thank you! Sure…my question is if I can do this entirely within KM using its Dictionary construct:

string Command1 = "COMMAND-G";
string Command2 = "OPTION-h";
string Command3 = "u";
dictionary myDictionary;
device virtualKeyboard;

myDictionary.Encode ("close window", Command1);
myDictionary.Encode ("open window", Command2);
myDictionary.Encode ("paint window", Command3);

virtualKeyboard.Output ( Dictionary.Decode ("paint window") );

Oh, so if I understood correctly: You want to create all the shortcuts for an app using only the Dictionary? Is that correct?

You could use an action with Switch case and %TriggerValue%, using these:

, , , , etc
TriggerValue uses these characters for Command, Option, Shift, Control, etc.

2 Likes

Hello and thank you, Hello. Got ya. Is there a way to then "type" the key command from KM?

Not that I know.
I keep them as a Comment so I can reference them later :slight_smile:

1 Like

As @hello has said, you could use a Switch Case and depending on the result of the data retrieved from the Dictionary yes, Keyboard Maestro could type them. By this method the actual data saved in the Dictionary could be in whatever form you like as long as you know what it means.

The image below is assuming the data retrieved from the Dictionary is passed to a Variable first (to keep the example simple). But you could alter so that the Switch Case works directly with the Dictionary by using a Text condition (and using the correct Dictionary syntax) rather than a Variable Condition.

image

2 Likes

Cheers on your example - that does the selection. I'm looking encode the keystrokes themselves in a dictionary. I have 130 separate key commands in the macro, and I'm reading them in from a plist file, to keep them manageable. Is there a way to do that?

It would be a very tedious task to set up a Switch Case with 130 different outcomes... but it would be possible if this goal of getting Keyboard Maestro to type hot keys is the only way you can accomplish your task.

As it all seems a bit left field - I'm wondering if there might be a better way to achieve whatever you are trying to achieve?

I had a quick reread of the threads above and I don't think you've stated what you are ultimately trying to do - i.e. why you need to type these hot keys. Apologies if I missed that.

2 Likes

notsteve,
it is not just a matter of keys but also what application they apply to. So it is most likely you have to use Applescript to accomplish the sending of keys to the specific application. I'm sure there are Applescript experts here that can help you. Is those keys specific to one application only or multiple applications ?

1 Like

Sounds like an interesting task, also would like to know in which app(s).

In AppleScript you can call KM scripts as:

on utils_do_something()
    tell application "Keyboard Maestro Engine"
    	do script "thelongstringofnumbers"
    end tell
end utils_do_something

it'd be interesting to know if you can pass the TriggerValue as well

1 Like

Hello, all. I've finished my code here, and written an abstraction layer for sending key commands from KM. Key Commands can be defined dynamically, as tokens.

A second layer of abstract added through CSV files, which store key commands, their modifiers, command names, etc. All key commands are supported (e.g. separate L%R command modifiers, brightness, and such—except one hardware key…I don't remember which. Or, maybe it does.

It's a nice system if you send a lot of key commands from KM to other applications—it's much easier to define them in a PLIST or CSV file. And of course you can dynamically switch between files, or change key command values in real-time.

The code is encapsulated from my application, but some tweaks might be desirable to make it feel more like a native KM feature. Or, it might be fine-as is. Tho…some people might prefer a part of it to be an Action, which would be simple enough.

If someone wants to jump into this, I'm happy to send. I don't know if it would be helpful to post as-is.

Thank you to the folks here for their kindness, and for Peter being so accessible. Quite a nice community here.

1 Like

Hi Zabobon, yes you're quite right, of course. The abstraction system solves this problem, collapsing a linear time algorithm to constant time; i.e., there are no comparison operations.

Can you share it, thanks!

1 Like

I'll have a think about the best way to do that.

I'll create a general-use macro for y'all, and post it.

After some thought, any implementation of this idea is arbitrary. It's perhaps most helpful if I post a macro template, and an Action. (I haven't tested the action, but it's very simple.)

Create your key code translation dictionary as a PLIST file, then use CJK's invaluable PLIST/Dictionary Action to read it in as one action.

I've documented the macro with more information and suggestions, including a reference to key codes.

Cheers!

Attached are:
Apple Key Codes.plist.zip (1.6 KB)
Send Key Command.kmlibrary (19.2 KB)
Send Key Command.zip (11.3 KB)

2 Likes