To map the application key to right-click at caret position using Karabiner-Elements

690 views Asked by At

To map the application key to right-click using Karabiner-Elements one can use:

"simple_modifications": [
    {
        "from": {
            "key_code": "application"
        },
        "to": {
            "pointing_button": "button2"
        }
    },

However, this will perform a right-click at the position of the mouse pointer. I want it to move the pointer to the position of the caret and then perform the right-click.
Presumably, to do this I need to use "complex_modifications" rule.
My main problem is I don't know how to change the mouse pointer position to the caret position. Using AutoHotkey there's an answer here Moving the mouse pointer to the cursor
I can't say I really understand the answer there but anyway I'm looking to do this with Karabiner-Elements

1

There are 1 answers

5
clemsam lang On

I think that you can move the mouse cursor to a specified position in newer 13+ versions of Karabiner-Elements (which I can't use with MacOS High Sierra).
I fear though, that it's very difficult to get x- an y-values of a selection, first.

BUT:
You can use the AppleScript UI attribute "AXFocusedUIElement" to right-click on selected UI elements.
(This DOES work fine for Finder windows files&folders and TextEdit selections, e.g., but sadly does not for Safari selections and files on Finder's desktop.)
If your main goal is to navigate inside Finder's windows this script* works perfectly.

{  "description": "Right-click via Karabiner-Elements/shell-command/AppleScript",
   "manipulators": [ {
            "type": "basic",
            "from": {
                "key_code": "left_arrow",
                "modifiers": {"mandatory": "left_control" }
                    },
              "to": [  {
 "shell_command": "osascript -e 'tell application \"System Events\" to set _/¯
  activeApp to name of first process whose frontmost is true \n tell application _/¯
  \"System Events\" to tell application process activeApp \n set mySelect _/¯
  to value of attribute \"AXFocusedUIElement\" \n tell mySelect to perform _/¯
  action \"AXShowMenu\" \n end tell'"
}  ]  }  ]
}

Notice that the entire osascript ( "osascript -e ' … >> … end tell'" ) needs to be a one-liner (I inserted "_/¯" for better readability).

(* the AppleScript "tell application … end tell" part is found in several variations on the net.
AND: Of course you can change my Ctrl-LeftArrow shortcut to any one you like.)