Sublime Forum

Is it possible to detect keystroke when quick panel is opened

#1

I have a plugin which uses show_quick_panel. Is it posssible to do something different when I hit, say, ctrl+enter to select an item instead of simply enter? Or any other keystroke.

There doesn’t seem to be anything in the API for this, but perhaps I can do something clever in the keymap? I tried something like

    { "keys": ["f3"], "command": "mru_plus", "context": [
        { "key": "last_command", "operator": "equal", "operand": "mru_plus" },
        { "key": "popup_is_visible" },
        { "key": "popup_has_focus" }
    ]},

but got no command for selector: noop:

mru_plus is a valid command that runs if I remove the context arg.

Thank in advance for any ideas

0 Likes

#2

Quick panels or Command Palette in general are queried by

{ "key": "overlay_has_focus", "operator": "equal", "operand": true }

Also found

{ "key": "overlay_name", "operator": "equal", "operand" : "goto" }

Not sure if it can handle other than built-in quick panels though.

0 Likes

#3

window.show_quick_panel() has a WANT_EVENT flag that causes it to tell you information on the key that was used to select the item from the panel via an Event. However, this requires Sublime Text >= 4096.

For example:

import sublime
import sublime_plugin

from sublime import QuickPanelFlags


class ExampleCommand(sublime_plugin.WindowCommand):
    def run(self):
        items = ["item1", "item2", "item3"]
        self.window.show_quick_panel(items, flags=QuickPanelFlags.WANT_EVENT,
            on_select=lambda idx,event: self.pick(event, items, idx))

    def pick(self, event, items, idx):
        if idx == -1:
            print("panel cancelled")
            return

        if event["modifier_keys"].get("ctrl"):
            print(f'picked {idx} ({items[idx]}) with control')
        else:
            print(f'picked {idx} ({items[idx]}) normally')
1 Like

#4

Thank you @OdatNurd !! This is way better than the hackish semi-solution that I came up with!

0 Likes