Replacing a hot key with a different hotkey

Hi! Is it possible to type a hot key (alt tab) and have KM replace it with another? (command tab)

running version 9 on a mac with an external ps2 compatible keyboard

1 Like

Did you try this approach? However you need to be aware that KM already comes with some builtin actions that are pre-activated and for this to work the way you want you will have to deactivate the builtin Alt-Tab macro.

Yes it's possible. I'd recommend something like Karabiner-Elements for pure key replacements though.

1 Like

Yes. I'm working in version 8, but there's no reason to think this won't still work in version 9.

use application id "com.stairways.keyboardmaestro.editor"
use application id "com.stairways.keyboardmaestro.engine"

use scripting additions

property macro : a reference to macro id (getvariable "id")
property trigger : a reference to trigger 1 of my macro

property hotkey : {MacroTriggerType:("HotKey") ¬
	, FireType:("Pressed"), KeyCode:missing value ¬
	, |Modifiers|:missing value}

property tab : 48
property cmd : 256
property alt : 2048

tell hotkey to set [its KeyCode, its |Modifiers|] to [tab, cmd]
tell application id "" to make new ¬
	property list item with properties {value:hotkey}
set my trigger's xml to the result's text

Hi CJK. I've seen many of your posts. You are one of the elite wizards on this site. I respect you. I should never question someone who is 10x smarter than me, because I'm probably wrong.

But why would you need some fancy AppleScript program when a simple Type Key action in KM suffices?

Also, the trigger for your macro is CMD Tab, but the user wants to change ALT-Tab to CMD-Tab.

Tag users using the @ symbol, and selected the username from the menu that appears as you type. Otherwise, if your settings for Discourse are like mine, you don't get notified without an actual tag made upon one's user handle.

You should always question people who are smarter than you (I'm not saying I am smarter than you), because people who are smarter generally don't ever mind being wrong: they usually became smart by being wrong, knowing when they were wrong, and learning how to correct themselves. In contrast, when you question people who are less smart, they often become defensive, because they see being wrong as a failing, and as confirmation of being less smart, which is not at all true. But the result is that they typically feel hurt and offended.

Plus, you also need to keep the smart people in check, and challenging their position helps ensure their heads remain on their shoulders and they don't get too much of a self-inflated ego.

I think you've got a great technique for approaching people when you want to point out a possible mistake they have made. As it turns out, I think you're right, and the mistake is looking quite likely to be mine.

Good spot. I took the screenshot after running the script, so that's why it shows instead of . I will assume the OP has enough grit to cope with this oversight.

Because I think one of us has misinterpreted the OP's wishes (or, rather, the OP was ambiguous in his request), and the more I pause to contemplate whether it's you or I that has misstepped, I am seeing now that someone else has replied suggesting a key-mapping solution; and the OP's decision to phrase it as "type a hot key" is something I overlooked. So I'm leaning towards thinking the error may have been mine.

To answer your question: I was originally working along the premise that the OP wasn't looking for a key-mapping to be performed; that is, he didn't want to press one key combo and have that behave as though he were pressing another. I interpreted his request as a wish to have a macro that is originally set up to be triggered by a particular hotkey that, upon pressing, would run the macro and, along the way, physically change the hotkey trigger so that a different combo is needed to execute the macro in the future (or perhaps, more usefully, target another macro, and alter its trigger).

There are quite a few differences between the two situations, so your solution, which is indeed very simple and elegant, isn't doing the same thing mine is, as I believe we were solving two different problems. In a key-mapping context, one loses hotkey real estate, as two key combinations are now entangled ino performing one automative task, i.e. whether he presses or he presses , the resulting effect will be identical. There are probably situations where this is beneficial, but at the time when I read his request, I think the notion of doing something like this possibly seemed, for me, too undesirable to contemplate, as I am constantly struggling to find convenient, available hotkeys to utilise.

Meanwhile, I also lingered on the OP's choice of word in describing his objective as one hotkey "replace[-ing]" the other, rather than, say, "combining", or "mirroring", or "simulating", etc. Thus I wrote a script that literally replaces one hotkey (trigger) with another, freeing up the original to be used elsewhere. But I also realise I've mentally inserted the word "trigger" whilst reading his post, so that's a comprehension failing on my part.

I'm still reasonably glad I drafted that script, as I think it will/may be useful for my own needs, and I'm now expanding it towards providing many convenience functions for editing macros from a script, as I do often find myself wanting to replace all instances of one variable with another, or insert identical actions into every macro that contains a script, etc. So this helps towards that end.


Thanks CJK. You could be a psychologist with all your thoughtful ways of dealing with people.

Smalltalk: Actually I find self-criticism to be one of the best ways to get people to like me (along with giving praise to others). I'm always trying to criticize myself before others do. It really works. I've been able to get complete strangers to feel very comfortable in my presence within 5 seconds of meeting them this way. I'm not really a very personable person but this trick really works. Back at work they call me Sheldon because I'm, in SO many ways, nearly every way, his doppleganger. Except he's a bit thinner than me. I think that's why I picked the name Sleepy when I joined this message board. I considered Dopey, but that was a little over the top, so I settled for Sleepy. I enjoy self-criticism as a technique; you don't have to take it too seriously. If anything my real problem is vanity, not humility.

Bigtalk. We don't really know if the user wanted a bi-directional key remapping or not. Ambiguities exist in nearly 100% of all user requests: we both know that. We always try to deal with it politely. Now that I think of it, my solution might not work bi-directionally because one macro's Type action may trigger the doppleganger's Type action, resulting in an infinite loop. I think that could be fixed easily enough by two additional actions: disabling a macro or group before and re-enabling it after. But even if my solution gets more complex, I think for the average person, especially the average person who has never posted on this website before, a KM macro with three actions is probably easier to understand than an AppleScript action. I'm able to dabble in AppleScript, but your script is way beyond my reach. I suspect your approach is more CPU-efficient and less laggy, but if performance isn't an issue, I think most people would take the KM solution.

Anyway thanks for your alternative solution. And thanks for saying "I think you've got a great technique for approaching people". That was nice.

1 Like