Home Download Buy Blog Forum Support

Check whether key is pressed to change behav of quick panel

Check whether key is pressed to change behav of quick panel

Postby gregor.hoch on Sat Nov 23, 2013 3:36 pm

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!
gregor.hoch
 
Posts: 144
Joined: Sat Oct 01, 2011 7:54 pm

Re: Check whether key is pressed to change behav of quick panel

Postby skuroda on Sun Nov 24, 2013 3:13 am

skuroda
 
Posts: 688
Joined: Fri Oct 05, 2012 8:11 am

Re: Check whether key is pressed to change behav of quick panel

Postby gregor.hoch on Sun Nov 24, 2013 9:36 am

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...
gregor.hoch
 
Posts: 144
Joined: Sat Oct 01, 2011 7:54 pm

Re: Check whether key is pressed to change behav of quick panel

Postby facelessuser on Sun Nov 24, 2013 4:02 pm

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.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Check whether key is pressed to change behav of quick panel

Postby gregor.hoch on Sun Nov 24, 2013 7:41 pm

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: http://macscripter.net/viewtopic.php?pid=114479#p114479
The binary is in the 'my-package' folder and here is the function:

Code: Select all
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).

Code: Select all
def on_done(i):
            if i < 0:
                return

            if self.check_modifier_keys():
                print('do something')               
            else:
                print('do something else')
gregor.hoch
 
Posts: 144
Joined: Sat Oct 01, 2011 7:54 pm

Re: Check whether key is pressed to change behav of quick panel

Postby facelessuser on Sun Nov 24, 2013 10:04 pm

Okay, I see what you wanted now. I have always wondered why this functionality isn't already in the sublime API.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests