Using Keyboard Maestro to Trigger Target Display Mode

I’m still using my now-ancient 2013 iMac in Target Display Mode as my primary monitor for work-from-home. Due to some muscle issues, I’ve switched from using Apple’s keyboards to using the Kinesis Freestyle Edge split keyboard with its tenting kit. I also invested in a basic USB 2.0 ‘switching’ hub so I can flip the keyboard between my iMac and MacBook Pro.

The problem: ⌘+F2 doesn’t trigger Target Display Mode from the Kinesis keyboard. I don’t want to have to keep pulling the Apple Magic Keyboard out of the drawer to switch the display.

Solution: I created a Keyboard Maestro macro that sends this AppleScript command.

tell application "System Events" to key code 144 using command down

I’m triggering it by the same ⌘+F2 hot key that should natively work.

Since my iMac is still on Mojave, there are some known bugs with Accessibility permissions. I ended up having to restart the Keyboard Maestro Engine a few times while cycling the Accessibility permissions in the Security & Privacy Preference Pane.