Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you map shortcut keys to output strings? #2949

Closed
GanZhiXiong opened this issue Dec 25, 2021 · 6 comments
Closed

Can you map shortcut keys to output strings? #2949

GanZhiXiong opened this issue Dec 25, 2021 · 6 comments

Comments

@GanZhiXiong
Copy link
Contributor

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

@Muirium
Copy link

Muirium commented Jan 9, 2022

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

#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
Copy link
Contributor Author

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
Copy link

Muirium commented Jan 10, 2022

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
Copy link
Contributor Author

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

@Muirium
Copy link

Muirium commented Jan 11, 2022

You’re welcome!

@eugenesvk
Copy link

eugenesvk commented Nov 17, 2022

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
  ]}
]}

AndreasNasman added a commit to AndreasNasman/.config that referenced this issue Apr 3, 2024
The benefit of using yabai to focus spaces is that you can switch focus
between already visible spaces, something the native "Switch to Desktop"
functionality lacks. I noticed, however, that this isn't really an
issue, since you can switch display focus with yabai without disabling
SIP (must sadly be disabled for space focusing 😭), which moves the
focus between already opened spaces when you're using multiple monitors.
When only using your laptop, you only have one display, so focusing a
visible space is redundant. The "Switch to Desktop" functionality is
also much faster than the yabai counterpart (at least when using fish).

One drawback from using "Switch to Desktop" is that it hijacks both
specified the left and right modifier keys. With the EurKEY layout, this
prevents us from typing e.g. the € sign with option-5. A workaround to
this is using Karabiner and remapping right_option-5 to type the € sign
for us! 🎉

Resources:
pqrs-org/Karabiner-Elements#2949 (comment)
pqrs-org/Karabiner-Elements#3157 (comment)
AndreasNasman added a commit to AndreasNasman/.config that referenced this issue Apr 9, 2024
AndreasNasman added a commit to AndreasNasman/.config that referenced this issue Nov 2, 2024
The benefit of using yabai to focus spaces is that you can switch focus
between already visible spaces, something the native "Switch to Desktop"
functionality lacks. I noticed, however, that this isn't really an
issue, since you can switch display focus with yabai without disabling
SIP (must sadly be disabled for space focusing 😭), which moves the
focus between already opened spaces when you're using multiple monitors.
When only using your laptop, you only have one display, so focusing a
visible space is redundant. The "Switch to Desktop" functionality is
also much faster than the yabai counterpart (at least when using fish).

One drawback from using "Switch to Desktop" is that it hijacks both
specified the left and right modifier keys. With the EurKEY layout, this
prevents us from typing e.g. the € sign with option-5. A workaround to
this is using Karabiner and remapping right_option-5 to type the € sign
for us! 🎉

Resources:
pqrs-org/Karabiner-Elements#2949 (comment)
pqrs-org/Karabiner-Elements#3157 (comment)
AndreasNasman added a commit to AndreasNasman/.config that referenced this issue Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants