pqrs-org / Karabiner-Elements

Karabiner-Elements is a powerful utility for keyboard customization on macOS Sierra (10.12) or later.
https://pqrs.org/osx/karabiner/
The Unlicense
18.82k stars 838 forks source link

Can you map shortcut keys to output strings? #2949

Closed GanZhiXiong closed 2 years ago

GanZhiXiong commented 2 years ago

For example, press the shortcut key in the text box, output "こんにちは".

Muirium commented 2 years ago

Yes. You need to spell out every single keystroke required, in a macro.

https://github.com/pqrs-org/Karabiner-Elements/issues/2711

Alternatively, you could use the shortcut to run a shell script which puts your desired string on the clipboard and then presses Command V to paste it in place. I do stuff like that in my macros, too.

GanZhiXiong commented 2 years ago

Thanks, 👍

Yes. You need to spell out every single keystroke required, in a macro.

But it can't output the text of a specific input method, such as automatically outputting "你好", and the json code will become very much.

Alternatively, you could use the shortcut to run a shell script which puts your desired string on the clipboard and then presses Command V to paste it in place. I do stuff like that in my macros, too.

Is there sample code? For example, press the letter a to output "abcdef". And cannot occupy the system clipboard.

Muirium commented 2 years ago

Without using the system clipboard? I don't know a way to avoid it, short of composing your entire string manually with a sequence of pressed keys.

Using the clipboard is easier. But Unicode text like 你好 is tricky. However, this works:

  "title": "Test Rules",
  "rules": [
    {
      "description": "Right Control 0 ⇨ Paste 你好",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "0",
            "modifiers": {
              "mandatory": [
                "right_control"
              ],
              "optional": [
                "caps_lock"
              ]
            }
          },
          "to": [
            {
              "shell_command": "osascript ~/.config/karabiner/assets/complex_modifications/Hello.scpt"
            },
            {
              "key_code": "v",
              "modifiers": [
                "right_command"
              ]
            }
          ]
        }
      ]
    }
  ]
}

This calls an AppleScript which puts the string on the clipboard, then presses paste. Here is that simple Hello.scpt:

set the clipboard to "你好"

I can't figure out how to make Unicode characters play nice, straight in the shell, which is why I dodged that with an AppleScript. Clunky but effective.

GanZhiXiong commented 2 years ago

Thank you very much for your reply. I tested your code and it works fine, it solves my problem. 👍👍👍

Muirium commented 2 years ago

You’re welcome!

eugenesvk commented 1 year ago

You can use the following script to paste text directly while also preserving the current clipboard content

{ "description" : "l⌥p ⟶ こんにちは","manipulators":[
{       "type":"basic",
 "from" :{"key_code":"p","modifiers":{"mandatory":["left_option"]}},
 "to"   :[{"shell_command":"osascript -e 'set temp to the clipboard \n set the clipboard to \"こんにちは\" \n tell application \"System Events\" \n   keystroke \"v\" using command down \n   delay 0.1 \n end tell \n set the clipboard to temp'"}]
} ]},

Though if you have many keybinds such as this one, it's best to use Goku with its much more ergonomic format that would look closer to this:

{:profiles {:goku {:default true}}
:templates { ; define the script monstrosity in one place as a template
  :sym "osascript -e 'set temp to the clipboard \n set the clipboard to \"%s\" \n tell application \"System Events\" \n   keystroke \"v\" using command down \n   delay 0.1 \n end tell \n set the clipboard to temp'"
 }

:main [
{:des "l⌥p ⟶ こんにちは" :rules [
  [:!Op [:sym "こんにちは"] ] ; simple print command
  ]}
]}