visual studio code - Binding a terminal command to a keybind, how can I focus on the terminal? - Stack Overflow

I have a keybind that triggers a terminal command. The terminal command prompts for input, but in order

I have a keybind that triggers a terminal command. The terminal command prompts for input, but in order to respond, I have to make an extra, unnecessary click to focus on the terminal. How can I avoid this so that when I trigger the keybind, the terminal command runs, and the VSCode focus automatically shifts to the terminal?

I found a solution, but I'd prefer not to press the keybind twice in a row; though it's still better than using the keybind and then clicking.

tasks.json

{
  "tasks": [
    {
      "label": "Run Example Command",
      "type": "shell",
      "command": "powershell",
      "args": [
        "..."
      ],
      "windows": {
        "command": "powershell",
        "args": [
          "..."
        ]
      }
    }
  ]
}

keybindings.json

{
  {
    "key": "f4",
    "command": "workbench.action.tasks.runTask",
    "args": "Run Example Command"
  },
  {
    "key": "f4",
    "command": "workbench.action.terminal.focus",
    "when": "taskRunning"
  },
}

How can I make the terminal receive focus on the first F4 press?

Update Illustration

By default what my problem With @Mark's second solution

I have a keybind that triggers a terminal command. The terminal command prompts for input, but in order to respond, I have to make an extra, unnecessary click to focus on the terminal. How can I avoid this so that when I trigger the keybind, the terminal command runs, and the VSCode focus automatically shifts to the terminal?

I found a solution, but I'd prefer not to press the keybind twice in a row; though it's still better than using the keybind and then clicking.

tasks.json

{
  "tasks": [
    {
      "label": "Run Example Command",
      "type": "shell",
      "command": "powershell",
      "args": [
        "..."
      ],
      "windows": {
        "command": "powershell",
        "args": [
          "..."
        ]
      }
    }
  ]
}

keybindings.json

{
  {
    "key": "f4",
    "command": "workbench.action.tasks.runTask",
    "args": "Run Example Command"
  },
  {
    "key": "f4",
    "command": "workbench.action.terminal.focus",
    "when": "taskRunning"
  },
}

How can I make the terminal receive focus on the first F4 press?

Update Illustration

By default what my problem With @Mark's second solution
Share Improve this question edited Mar 12 at 18:42 rozsazoltan asked Mar 12 at 12:10 rozsazoltanrozsazoltan 11.3k6 gold badges20 silver badges59 bronze badges 4
  • This is probably the issue: you cannot have conflicting key bindings. F4 should be only one. – Sergey A Kryukov Commented Mar 12 at 12:12
  • @SergeyAKryukov you misunderstood. This is a working example. The original problem was F4 + click, and I found a solution with a double F4 press. However, the issue still bothers me because the expected result is achieving it with a single F4 press. --- Since the double F4 press is still more efficient than F4 + click, I will use it. But I'd be interested to know if anyone has a solution to make both the focus switch and the command execution happen with a single F4 press. – rozsazoltan Commented Mar 12 at 12:14
  • You did not explain my misunderstanding. You confirmed that this is the working example and provided keybindings.json or its fragment. But you should not such a file with the duplicate key. What happens if you use a different key for workbench.action.terminal.focus? – Sergey A Kryukov Commented Mar 12 at 12:20
  • Now, in my understanding, you want to combine workbench.action.tasks.runTask with later workbench.action.terminal.focus, when the task is already running, But it means that you have to perform focusing triggered by the running task or defer focusing. Note that you also make sure that the terminal is visible. In all cases, you still need one command and one key binding with F4. Again, in my understanding, you can achieve something like that by writing a VSCode extension. – Sergey A Kryukov Commented Mar 12 at 12:24
Add a comment  | 

1 Answer 1

Reset to default 1

There may be two solutions - which I can't test without your actual powershell command and args.

Combine your commands into one keybinding using the runCommands command:

{
  "key": "alt+c",
  "command": "runCommands",
  "args": {
    "commands": [
      {
        "command": "workbench.action.tasks.runTask",
        "args": "echo"
      },
      "workbench.action.terminal.focus"
    ]
  }
}

Or try adding presentation options to your task:

  "tasks": [
    {
      "label": "echo",
      "type": "shell",
      "command": "powershell",
      "args":
        [...],
      "presentation": {
        "echo": true,
        "reveal": "always",    // note
        "focus": true,         // see if this works
        "panel": "new",
        "showReuseMessage": false,
        "clear": true
      }
    }
  ],

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744753424a4591736.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信