0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Macのチャットツールの「送信」をCommand+Enterキーに統一する方法(Karabiner-Elements活用)

Last updated at Posted at 2025-02-02

「Enter」で勝手に送信される問題

Macのチャットツール(Messenger、Discord、ChatGPT など)では、デフォルトの Enter キーの挙動が「送信」になっている場合が多く、入力中の文章が途中でうっかり送信されてしまった・・・なんてことが多々あります。
アプリ内の設定で変更可能なものは良いのですが、変更できない場合は事故に繋がる可能性も。

そこで本記事では、Enter で改行し、Command+Enter で送信する設定Karabiner-Elements を用いて実装する方法を紹介します。

この方法は 複数のチャットツールに適用可能 であり、個別にカスタマイズできます。

1. Karabiner-Elements のインストールと基本設定

まず、以下のURLからKarabiner-Elementsをダウンロード・インストールしてください。

インストール後の設定方法は以下の記事が参考になります。

2. JSONファイルの設定

Karabiner-Elements のインストールと基本設定が完了したら、設定ファイルを準備します。

2-1. 設定ファイルの保存場所

Karabiner-Elements では、カスタム設定を ~/.config/karabiner/assets/complex_modifications/ ディレクトリ内に新たなJSONファイルとして保存できます。

各チャットアプリ専用の設定ファイルを作成し、適用することで柔軟なカスタマイズが可能です。

2-2. 基本となるサンプルコード

以下が各チャットアプリ専用の設定ファイルを作成する基本的な流れです。

  1. ~/.config/karabiner/assets/complex_modifications/ ディレクトリ内に新規JSONファイルを作成します。
  2. 作成したJSONファイルに以下の記述をします。以下のJSONは、様々なチャットツールに適用可能な基本的なキー設定のテンプレートです。
    • titledescription は管理しやすい任意の文字列を入力してください。分からない場合は APP_NAME を対象のアプリ名に置き換えればOKです。
    • APP_BUNDLE_IDENTIFIER は対象のアプリの bundle_identifier(アプリID)に置き換えてください。bundle_identifier(アプリID)の取得方法は次の項目で説明します。
{
    "title": "Fix APP_NAME Keybindings",
    "rules": [
        {
            "description": "Use Shift+Enter for newline, Command+Enter for sending in APP_NAME",
            "manipulators": [
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter"
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter",
                            "modifiers": ["left_shift"]
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^APP_BUNDLE_IDENTIFIER$"
                            ]
                        }
                    ]
                },
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter",
                        "modifiers": {
                            "mandatory": ["command"]
                        }
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter"
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^APP_BUNDLE_IDENTIFIER$"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

3. アプリIDの取得方法

チャットツールごとに適用するには、それぞれの bundle_identifier(アプリID)が必要です。
bundle_identifier は以下の手順で取得できます。

3-1. Karabiner-EventViewer を使用する方法

Karabiner-Elementsと同時にインストールされたKarabiner-EventViewerを使用することで、bundle_identifier(アプリID)の取得が可能です。

  1. Karabiner-EventViewer を開く。
  2. 左側のメニューから Variables を選択。
  3. 設定したいアプリを前面に表示した状態で表示されたコードを確認。
  4. frontmost_application の情報として bundle_identifier(アプリID)が表示される。

例(Messengerアプリの場合):
CleanShot 2025-02-02 at 08.17.30@2x.png

3-2. ターミナルを使用する方法

以下のコマンドを実行すると、現在アクティブなアプリの bundle_identifier を取得できます。

osascript -e 'id of application "アプリ名"'

例(Messengerアプリの場合):

osascript -e 'id of application "Messenger"'

これにより、 com.facebook.archon という結果が得られます。

4. 各チャットツールへの適用例

ここではサンプルコード元に実際のアプリへの適用例を記載します。

4-1. Messenger

アプリID

Messengerの bundle_identifiercom.facebook.archon です。

設定ファイル

~/.config/karabiner/assets/complex_modifications/messenger.json

{
    "title": "Fix Messenger Keybindings",
    "rules": [
        {
            "description": "Use Shift+Enter for newline, Command+Enter for sending in Messenger",
            "manipulators": [
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter"
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter",
                            "modifiers": ["left_shift"]
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^com\\.facebook\\.archon$"
                            ]
                        }
                    ]
                },
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter",
                        "modifiers": {
                            "mandatory": ["command"]
                        }
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter"
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^com\\.facebook\\.archon$"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

4-2. Discord

アプリID

Discordの bundle_identifiercom.hnc.Discord です。

設定ファイル

~/.config/karabiner/assets/complex_modifications/discord.json

{
    "title": "Fix Discord Keybindings",
    "rules": [
        {
            "description": "Use Shift+Enter for newline, Command+Enter for sending in Discord",
            "manipulators": [
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter"
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter",
                            "modifiers": ["left_shift"]
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^com\\.hnc\\.Discord$"
                            ]
                        }
                    ]
                },
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter",
                        "modifiers": {
                            "mandatory": ["command"]
                        }
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter"
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^com\\.hnc\\.Discord$"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

4-3. ChatGPT

アプリID

ChatGPTの bundle_identifiercom.openai.chat です。

設定ファイル

~/.config/karabiner/assets/complex_modifications/chatgpt.json

{
    "title": "Fix ChatGPT Keybindings",
    "rules": [
        {
            "description": "Use Shift+Enter for newline, Command+Enter for sending in ChatGPT",
            "manipulators": [
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter"
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter",
                            "modifiers": ["left_shift"]
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^com\\.openai\\.chat$"
                            ]
                        }
                    ]
                },
                {
                    "type": "basic",
                    "from": {
                        "key_code": "return_or_enter",
                        "modifiers": {
                            "mandatory": ["command"]
                        }
                    },
                    "to": [
                        {
                            "key_code": "return_or_enter"
                        }
                    ],
                    "conditions": [
                        {
                            "type": "frontmost_application_if",
                            "bundle_identifiers": [
                                "^com\\.openai\\.chat$"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

5. 設定ファイルを作成した後の操作

作成したJSONを Karabiner-Elements で実行します。

  1. Karabiner-Elements を開く。
  2. Complex Modifications タブを開く。
  3. Add predefined rule をクリック。
  4. Import more rules from the Internet の下にある 自分で作成したルールを「Enable」して選択。CleanShot 2025-02-02 at 08.38.32@2x.png
  5. 適用するルールのトグルをオンにして有効化。CleanShot 2025-02-02 at 08.38.56@2x.png
  6. 対応するチャットアプリを再起動して、動作を確認。

設定後、適用されない場合は、Karabiner-Elements を一度終了し、Macを再起動すると適用されることがあります。

まとめ

以上、Karabiner-Elementsを活用して、Macのチャットツールを Enter で改行し、Command+Enter で送信する設定 に変更する方法を紹介しました。
この方法は Shift+Enter で改行、Enterで送信する仕様のアプリであれば、例に挙げたチャットアプリ以外にも適用可能です。Enterでうっかり送信されてしまうアプリに出会ったら容赦無く撲滅 設定変更していきましょう!

(Webブラウザ版はKarabiner-Elements では設定できないため、Tampermonkey を使用した方法を模索中です。天下統一までの道のりは長そうです…)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?