Sublime Forum

Check whether key is pressed to change behav of quick panel

#1

Hi,

I would like to check whether a certain key is pressed when the user selects an item from the quick panel so that I can change what happens with the selection. Imagine a quick panel that shows a list of files. Selecting one usually inserts the path at the current curser location. However, if the user is pressing cmd (or any other key) while selecting an item in the quick panel, the relative path is inserted at the current curser location. The example is arbitrary but you get the idea. Alfred does something like this and I think it would be a very nice feature to make the quick panel better! Any ideas how to implement this in python? I googled for ‘python key pressed’ and some suggestions came up but they all seem to be rather complex involving a GUI library.

Thanks!

0 Likes

#2

Take a look at github.com/facelessuser/FuzzyFi … l-features

0 Likes

#3

Thanks! That is very helpful and might be a solution. I still have to check the code more thoroughly though. It’s more complicated then I had hoped though. I thought I can just have an on_done function that checks whether command or option is currently pressed…

0 Likes

#4

I agree it seems overly complicated. The big things are:

  • Identify when the quick panel is open. This is the most annoying part, and it needs to be done well or your shortcuts can trigger when the panel is closed. What I did feels overly complicated to identify quick panels, but, at the time, it was the road I went down. Now that is working, I haven’t bothered to find a simpler way. If you find a less complicated way, please do share.

  • Give your key commands context via on_querry_command so the shortcuts only trigger only when the panel is open. That is why step 1 is so critical.

  • Lastly, have on_querry_command launch the appropriate command with the required arguments.

I feel all but the first step are pretty straight forward. But that first step took a bit to get it working reliably.

0 Likes

#5

It’s a nice workaround for FuzzyFileNav but my goal is a little simpler and I came up with a solution that only works on Mac OS but probably can be extended. It’s based on this command line interface to check for modifier keys on Mac os: macscripter.net/viewtopic.php?pid=114479#p114479
The binary is in the ‘my-package’ folder and here is the function:

def check_modifier_keys(self, key='cmd'):
        path = os.path.join(sublime.packages_path(), "my-package", "checkModifierKeys")
        p = subprocess.Popen([path, key], stdout=subprocess.PIPE)
        out, err = p.communicate()
        return out==b'1\n'

By default it checks for the cmd key but it also works for option, control, shift, and capslock. Using this function my on_done function passed to show_quick_panel looks like this. It executes different code depending on the modifier key so that I can have one quick panel that does two different things (e.g. FuzzyFileNav could reveal the file in the finder, copy the path or open it in ST all from a single quick panel).

def on_done(i):
            if i < 0:
                return

            if self.check_modifier_keys():
                print('do something')                
            else:
                print('do something else')
0 Likes

#6

Okay, I see what you wanted now. I have always wondered why this functionality isn’t already in the sublime API.

0 Likes