How to set a keyboard shortcut for opening a tool-window?

How can I set a keyboard shortcut for Activating toolwindow action?

I can’t write the action on plugin.xml and I do not want it hard-coded like this:

val shortcutUsed = keymap.getShortcuts(ACTIVATE_TOOL_WINDOW_ACTION_ID).any { it == DEFAULT_TOOLWINDOW_SHORTCUT }
        if (!shortcutUsed) {
            keymap.addShortcut(ACTIVATE_TOOL_WINDOW_ACTION_ID, DEFAULT_TOOLWINDOW_SHORTCUT)
        }

Hi,
it is customary that only built-in tool windows have keyboard shortcuts: generally Cmd/Alt+.

Can you elaborate on the “I can’t write the action on plugin.xml bit”?

Maybe you could use a “keymaps” entry in your plugin or even create a related independent keymap plugin.

I can’t write

<action id="ActivateXXXToolWindow">
 <keyboard-shortcut ...
</action>

because it is missing the class attribute. I can’t use <reference ref=... because it does not take <keyboard-shortcut.

Also, semi-related, <group id=.. class=...> does not take <keyboard-shortcut subelement and it should.

Can you provide an example? where should the keymaps entry be?

Maybe you could refer to: intellij-community/platform/platform-resources/src/keymaps at master · JetBrains/intellij-community · GitHub

I build my own keymap plugin based on keymap entry.

But why should it be a separate plugin for having 2-3 shortcuts for my plugin..
I just would like a default shortcut for a few actions.

It could also implement entry in your plugin.xml like this.

<extensions defaultExtensionNs="com.intellij">
    <bundledKeymap file="(your keymap).xml"/>
</extensions>

I tried that, but it does not “pick it up”.
Do you have an example how to use bundledKeymap?

What do you means of “pick it up”? Can you see your keymap in IDE setting.

As a somewhat unorthodox approach, you can inherit from AnAction and activate the tool window programatically in actionPerformed(). Then, a keyboard shortcut can be assigned in the plugin.xml to this action.

In your plugin.xml, add the following to the <actions>…</actions> section:

<action
    id="OpenYourToolWindowActionId"
    class="OpenYourToolWindowAction"
    text="Open Your Tool Window"
    description="Opens your tool window"
    icon="yourToolWindow.svg">
    <keyboard-shortcut
        first-keystroke="desired keystroke"
        keymap="$default"/>
</action>

Then implement OpenYourToolWindowAction as:

public class OpenYourToolWindowAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        if (project != null) {
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(YourToolWindow.ID);
            if (toolWindow != null) {
                ContentManager contentManager = toolWindow.getContentManager();
                Content content = contentManager.getSelectedContent();
                if (content != null) {
                    toolWindow.show();
                }
            }
        }
    }
}

That may not be 100% perfect, but it should get you very close.

1 Like