LoginSignup
1
2

More than 5 years have passed since last update.

[Visual Studio Code][Linux][xdotool] タスク/コマンドラインから画面の切り替えなしで別のアプリケーションに特定キー入力を送る

Posted at

編集はVSCodeでやってるんですが、クロスビルドとリモートデバック実行のためだけに、Eclipse(の派生IDE)で実行を行わないといけなかったので、VSCodeからウィンドウを切り替えないで実行できないか調べました。

Linux環境では xdotool というものを使えばコマンドラインから別のアプリケーションにキー入力を送れたので実現できました。

tasks.json 設定例

tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "sh",
    "args": [
        "-c"
    ],
    "isShellCommand": true,
    "showOutput": "always",
    "tasks": [
        {
            // need 'apt-get install xdotool'
            "taskName": "Test",
            "isTestCommand": true,
            "suppressTaskName": true,
            "args": [
                "xdotool search --name 'Eclipse' windowfocus --sync %1 key 'ctrl+F11' && xdotool search --name 'Visual Studio Code' windowactivate --sync %1"
            ]
        }
    ]
}

解説

実行には xdotool が必要です。おそらくX11環境下では使えるんじゃないかと思います。(確認はUbuntuのUnity環境下のみ)

apt
sudo apt-get install xdotool

上記のタスクでは以下の2つのコマンドを実行しています。

eclipseにctrl+F11を送信後、フォーカスをVSCodeに戻す
$ xdotool search --name 'Eclipse' windowfocus --sync %1 key 'ctrl+F11'
$ xdotool search --name 'Visual Studio Code' windowactivate --sync %1

--name のところは、指定した文字列がウィンドウタイトルに含まれていれば良いです。
1つ目のコマンド実行後にフォーカスが移ってしまっているので、2つ目のコマンドでもとに戻しています。
(VSCodeを2つ立ち上げてると誤動作するかもです(未確認))

参考

Ubuntu Manpage: xdotool - command-line X11 automation tool
音声と xdotool でキーボードとマウスのアクションをトリガーする
xdotoolでシェルからブラウザをリロードするワンライナー - Qiita
x11 - How to send keystrokes (F5) from terminal to a process? - Unix & Linux Stack Exchange
Eclipseショートカットキー Mac & Windows 対応表 - CODESCRIBBLE

1
2
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
1
2