3

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

Trevor
  • 525
  • 2
  • 6
  • 19

1 Answers1

1

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.)

clemsam lang
  • 440
  • 3
  • 11
  • Thanks for the snippet, the problematic part is the applescript which I can't get to work properly. If I run the following snippet. set theFrontMost to name of current application. tell application "System Events". set visible of process theFrontMost to false. set theFrontMost to first application process whose frontmost is true. set theControl to value of attribute "AXFocusedUIElement" of theFrontMost. tell theControl to perform action "AXShowMenu". end tell. I will work on Chrome but not on most other apps, Slack, vsCode, Word etc. – Trevor Nov 10 '21 at 14:07
  • set theFrontMost to name of current application.@ tell application "System Events". @ set visible of process theFrontMost to false. @ set theFrontMost to first application process whose frontmost is true. @ set theControl to value of attribute "AXFocusedUIElement" of theFrontMost. @ tell theControl to perform action "AXShowMenu". @ end tell. @ I will work on Chrome but not on Slack, vsCode, Word etc. I couldn't get the new lines working, using @ for newline – Trevor Nov 10 '21 at 14:16
  • Well, I noticed myself that "perform action "AXShowMenu"" does not work in every environment . Please try "\n click at mySelect\n ..." ! – clemsam lang Nov 10 '21 at 15:04
  • @clemsam_lang "click at mySelect" doesn't work for me. Googling for this, not from the Karabiner-Elements point of view. [This Macscripter post](https://www.macscripter.net/viewtopic.php?pid=200987) seems to sum it up. "To make GUI Scripting and access the contextual menu could be tricky. I have test this on Safari and Finder and every application is different." Chrome needs one solution, Word another, Slack, another and vsCode another. Not that practical, which is a shame. I feel apple really messed up by leaving out an application key. – Trevor Nov 14 '21 at 13:39
  • 1
    Okay, you're probably right, about Apple(Scripts, also)'s refusal to support mouse event management. But, as I'm writing this anyway, I looked into my last comment and notice that I should have written: "\n click at mySelect using {control down}\n …" BUT: I'm not SURE that "click at" takes an addition like "using {…}" at all; just to have mentioned it for whoever (as left-mouse-click-plus-control results in right-mouse-click with my MacBook). – clemsam lang Nov 14 '21 at 14:57