Need a Simple Way to Handle Modifiers for Non-ASCII Keys

I'm trying to intercept some keys like Home and End, among many others, and do special processing on them, but then pass the key back to the app that the user has active. In order to intercept them I need to use the KM "HotKey trigger" (right?) Then, because intercepting hotkeys "eats" the key (unlike ASCII keys using the KM ASCII-based "typed text" trigger), and because I want to pass the key back to the active application, I need to manually send the key to the app using the "Type a Keystroke" Action, right? The problem is that each key has four modifiers (I'm ignoring CAPS LOCK, which I don't consider a modifier.) And 2^4 is 16, so I need 16 IF statements like the following for each key that I want to intercept. This is certainly possible, but rather ugly. Is there a prettier way to handle this? Because 16 times about 40 hotkeys means a KM macro with about 600 actions. I'm not eager to type that many actions if there's a cleaner way.

image

I just discovered this action which may help me solve my problem:

image

I find that the above action isn't working (yes, I do have "Press and Hold" selected.) Someone else reported that the action wasn't working here, four years ago:

And the response was to not use this action. Well that advice doesn't work for me, for reasons stated in my first post. So can someone confirm the above post that the "Type the Option Modifier (and Press and Hold until end of macro)" action doesn't work? Why was the user told not to use this method to press modifiers?

I found an AppleScript command that looks like this:

tell application "app-name" to key code 124 using option down

The problem is that the word "option" appears to be an AppleScript keyword, not a variable/string, so it doesn't give me any advantage over KM, which also requires hardcoded modifiers.

Hey @Sleepy,

You can compose AppleScript in text on the fly and then run it.

set modKey to "option"

set asCmdStr to "
tell application \"System Events\"
   tell application process \"BBEdit\"
      key code 124 using " & modKey & " down
   end tell
end tell
"

run script asCmdStr

-Chris

That's a wonderful "level of indirection" I knew nothing about. That could solve my problem then. THANKS!

1 Like