如何编写一个复杂的修改程序,以使特定的键组合被按两次。

huangapple go评论54阅读模式
英文:

How to write a complex modification for Karabiner-Elements to make certain key combination get pressed twice

问题

您好,

以下是您提供的文本的翻译:

大家好,

我过去经常在按下ctrl+space(选择Mac键盘设置中的上一个输入源)时在三种语言(韩语、英语、德语)之间切换,这真的很烦人。后来我找到了Karabiner-Elements,并在Mac m1 pro上使用了几周,但对于使用中文、日文、韩文和越南文(CJKV)的用户存在问题。

(请查看以下链接,了解有关此问题的信息:

https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/to/select-input-source/

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

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

问题是菜单栏显示我正在使用韩语,但如果我键入,结果是上一个语言。假设我正在使用德语,然后按下某个键组合切换到韩语,菜单栏会显示已更改为韩语,但键盘布局仍然是德语。

这并不总是发生,但当它发生时,我必须按两次ctrl + space才能再次切换(从韩语切换到德语)并(从德语切换到韩语)。然后我可以使用韩语键盘布局。这是我当前的解决方法。

现在我成功制作了自己的Karabiner-Elements的复杂修改代码。我使用:

  • 右命令键切换到英语
  • 右Shift键切换到德语
  • 右选项键切换到韩语

但我想要编写类似于以下的JSON文件:
如果我按下右选项键切换到韩语,Ctrl + Space组合会自动按两次,以便自动执行解决方法,而无需我自己按两次Ctrl + Space。

这种编写JSON文件的可能吗?

以下是我的Command-Launching.json文件:

{
    "title": "私人设置语言等",
    "rules": [
  
        { "description": "右命令键到美国", 
          "manipulators": [ 
            { 
                "from": { 
                    "key_code": "right_command", "modifiers": { 
                        "optional": [
                             "any" 
                             ] 
                    } 
                }, "to": [ 
                    { "key_code": "right_command", "lazy": true } 
                ], 
                "to_if_alone": [ 
                    { "select_input_source": { "language": "en" } } 
                ], 
                "type": "basic" 
            }
        ] 
        }, 
        
        {   "description": "右Shift键到DE", 
            "manipulators": [ 
                { "from": { 
                    "key_code": "right_shift", "modifiers": 
                    { "optional": [ 
                        "any" 
                        ] 
                    } 
                }, "to": [ 
                { "key_code": "right_shift", "lazy": true } 
                ], 
                "to_if_alone": [ 
                    { "select_input_source": { "language": "de" } } 
                ], 
                    "type": "basic" 
                } 
            ] 
        },
        
        {   "description": "右选项键到KO", 
            "manipulators": [ 
                { "from": { 
                    "key_code": "right_option", 
                    "modifiers": 
                    { "optional": [ 
                        "any" 
                        ] 
                    } 
                }, "to": [ 
                { "key_code": "right_option", "lazy": true } 
                ], 
                "to_if_alone": [ 
                    { "select_input_source": { "language": "ko" } } 
                ], 
                    "type": "basic" 
                }
            ] 
        }
    ]
}

祝您一切顺利。

英文:

Good day to you all,

I used to switch between 3 languages (Korean, English, German) while pressing ctrl+space (select the previous input source in mac keyboard setting) which is really annoying. Then I found Karabiner-Elements and use it on Mac m1 pro since a few weeks but there is an issue for users who use the language Chinese, Japanese, Korean, and Vietnamese (cjkv)

(Check these links referring to the issue:

https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/to/select-input-source/

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

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

The issue is that the menubar shows that I am using Korean but if I type, the result is the previous language. Let's say: If I am using German and switch to Korean by pressing a certain key combination, the menubar says it is changed to Korean but the keyboard layout is still German.

It does not happen always but when it happens, I have to press ctrl + space twice to go (from Korean to German) and (from German to Korean) again. Then I can use a Korean keyboard layout. That is my current workaround.

Now I managed to make my own complex modification code for Karabiner-Elements. I use

  • right command to switch to English

  • right shift to switch to German

  • right option to switch to Korean.

But I want to code json file like this:
If I press on right option to switch to Korean, ctrl + space combination gets pressed twice automatically so that the workaround happens automatically without me having to press ctrl + space twice by myself.

Is this even possible to code a json file like that?

Below, you will see my Command-Launching.json file:

{
    "title": "private setting for language etc.",
    "rules": [
  
        { "description": "Right Command to US", 
          "manipulators": [ 
            { 
                "from": { 
                    "key_code": "right_command", "modifiers": { 
                        "optional": [
                             "any" 
                             ] 
                    } 
                }, "to": [ 
                    { "key_code": "right_command", "lazy": true } 
                ], 
                "to_if_alone": [ 
                    { "select_input_source": { "language": "en" } } 
                ], 
                "type": "basic" 
            }
        ] 
        }, 
        
        {   "description": "Right Shift to DE", 
            "manipulators": [ 
                { "from": { 
                    "key_code": "right_shift", "modifiers": 
                    { "optional": [ 
                        "any" 
                        ] 
                    } 
                }, "to": [ 
                { "key_code": "right_shift", "lazy": true } 
                ], 
                "to_if_alone": [ 
                    { "select_input_source": { "language": "de" } } 
                ], 
                    "type": "basic" 
                } 
            ] 
        },
        
        {   "description": "Right Option to KO", 
            "manipulators": [ 
                { "from": { 
                    "key_code": "right_option", 
                    "modifiers": 
                    { "optional": [ 
                        "any" 
                        ] 
                    } 
                }, "to": [ 
                { "key_code": "right_option", "lazy": true } 
                ], 
                "to_if_alone": [ 
                    { "select_input_source": { "language": "ko" } } 
                ], 
                    "type": "basic" 
                }
            ] 
        }
    ]
}

Best regards

I tried to find if-else condition statement but nothing really helped.

答案1

得分: 1

我发现根据Karabiner-Elements文档,直接在"command-launching.json"中按照我所希望的方式进行修改是不可能的。

有另一种解决方法。不要在命令to_if_alone中使用"repeat": 2,而是按照以下步骤操作:

  1. 创建一个Shell脚本,我们称其为run_command_twice.sh,并将其放在已知位置(如您的主目录):
#!/bin/bash
osascript -e 'delay 0.01' -e 'tell application "System Events" to keystroke (key code 49 using control down)'
osascript -e 'delay 0.01' -e 'tell application "System Events" to keystroke (key code 49 using control down)'
  1. 确保给予您的脚本执行权限:
chmod +x ~/run_command_twice.sh
  1. 然后修改您的command-launching.json以调用此脚本:
{
  ...
  "to_if_alone": [
    {
      "shell_command": "~/run_command_twice.sh"
    }
  ],
  ...
}

注意:

  1. 这样,当您单独按下右Option键时,它将调用Shell脚本,然后Shell脚本将运行osascript命令两次。
  2. 记得将~/run_command_twice.sh替换为您脚本的实际路径。
英文:

I found out that it is impossible to modify directly in "command-launching.json" as I wish according to the Karabiner-Elements documentation.

There is another workaround. Instead of using "repeat": 2 in the command to_if_alone, follow the steps:

  1. Create a shell script, let's call it run_command_twice.sh, and put it in a known location (like your home directory):

     #!/bin/bash
     osascript -e 'delay 0.01' -e 'tell application "System Events" to keystroke (key code 49 using control down)'
     osascript -e 'delay 0.01' -e 'tell application "System Events" to keystroke (key code 49 using control down)'
    
  2. Make sure to give execute permissions to your script:

    chmod +x ~/run_command_twice.sh
    
  3. Then modify your command-launching.json to call this script:

    {
      ...
      "to_if_alone": [
        {
          "shell_command": "~/run_command_twice.sh"
        }
      ],
      ...
    }
    

> NOTES:
> 1. This way, when you press the right_option key alone, it will call the shell script which in turn will run the osascript command twice.
> 2. Remember to replace ~/run_command_twice.sh with the actual path to your script.

huangapple
  • 本文由 发表于 2023年5月17日 06:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定