Home Download Buy Blog Forum Support

Retrieve Key Bindings

Retrieve Key Bindings

Postby ostinelli on Fri Apr 13, 2012 4:25 pm

Dear all,

once I've defined key bindings for a plugin, is there anyway to retrieve them from the plugin itself, for instance when a developer modifies them?

For instance, given the key bindings here below, how can I retrieve "super+shift+c" if I want to know which key binding can trigger "test2"?

Code: Select all
[
   { "keys": ["super+f8"], "command": "test" },
   { "keys": ["super+shift+c"], "command": "test2" }
]

Thank you,

r.
ostinelli
 
Posts: 15
Joined: Thu Apr 12, 2012 4:50 am

Re: Retrieve Key Bindings

Postby nick. on Fri Apr 13, 2012 4:43 pm

I'm not sure what you're asking...

You can type sublime.log_commands(True) in the console to see what command a keybinding executes.
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Retrieve Key Bindings

Postby ostinelli on Fri Apr 13, 2012 5:01 pm

what i am asking in detailed in the first post :)

thank you for your input, but isn't log_commands logging all of these to the console? i need to know programmatically the key bindings associated to a command, so if a developer modifies them, my plugin can still succesfully give the right tips on which keys to press on specific situations.

hopefully this makes more sense ^^_

r.
ostinelli
 
Posts: 15
Joined: Thu Apr 12, 2012 4:50 am

Re: Retrieve Key Bindings

Postby agibsonsw on Sat Apr 14, 2012 12:31 am

I figure it should be possible:

Code: Select all
def_keys = sublime.load_settings('Default (Windows).sublime-keymap')


I'm not sure how to then parse this object to find "command" : "your_command", but it's just JSON.

I don't know if there is a more direct root :?:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Retrieve Key Bindings

Postby ostinelli on Sun Apr 15, 2012 4:22 pm

Thank you,

I'm going to try this and will post here code, if successful.

:)

r.
ostinelli
 
Posts: 15
Joined: Thu Apr 12, 2012 4:50 am

Re: Retrieve Key Bindings

Postby ostinelli on Sun Apr 15, 2012 5:07 pm

So.. unfortunately I cannot find a way to make this work, since key bindings are defined in an array, not in a dict (which I suspect is what the sublime.Settings.get(Name) uses to retrieve the values.

Any other ideas? :)

r.
ostinelli
 
Posts: 15
Joined: Thu Apr 12, 2012 4:50 am

Re: Retrieve Key Bindings

Postby svenax on Sun Apr 15, 2012 10:59 pm

Hmm, still not really understading what you want to do. But to get the key code(s) corresponding to a given command you could try something like
Code: Select all
all_keys = sublime.load_settings('Default (Windows).sublime-keymap')
command  = 'some_command'
command_keys = [key for key in all_keys if key['command'] == command]

But maybe you want something completely different?
svenax
 
Posts: 152
Joined: Tue Dec 20, 2011 12:47 pm

Re: Retrieve Key Bindings

Postby ostinelli on Sun Apr 15, 2012 11:54 pm

svenax wrote:Hmm, still not really understading what you want to do. But to get the key code(s) corresponding to a given command you could try something like
Code: Select all
all_keys = sublime.load_settings('Default (Windows).sublime-keymap')
command  = 'some_command'
command_keys = [key for key in all_keys if key['command'] == command]

But maybe you want something completely different?


No that's exactly what I'd like :) Unfortunately:

Code: Select all
TypeError: 'Settings' object is not iterable
ostinelli
 
Posts: 15
Joined: Thu Apr 12, 2012 4:50 am

Re: Retrieve Key Bindings

Postby svenax on Mon Apr 16, 2012 10:15 am

ostinelli wrote:No that's exactly what I'd like :) Unfortunately:

Code: Select all
TypeError: 'Settings' object is not iterable


Ah, sorry, I didn't check that. Just assumed the snippet above worked. OK, that makes things a bit more complicated.

A simple approach like this mostly works:
Code: Select all
import sublime
import json

file = '/User/Default (Windows).sublime-keymap'
command = 'some_command'
json_data = json.load(open(sublime.packages_path() + file))
command_map = [key for key in json_data if key['command'] == command]


However, the Python json parser does not allow for comments, which are used in most default files. To handle that, you need a custom decoder object.
svenax
 
Posts: 152
Joined: Tue Dec 20, 2011 12:47 pm

Re: Retrieve Key Bindings

Postby bizoo on Mon Apr 16, 2012 10:28 am

svenax wrote:However, the Python json parser does not allow for comments, which are used in most default files. To handle that, you need a custom decoder object.

I use this code:
Code: Select all
RE_COMMENTS = re.compile('((//.*?$)|(/\*.*\*/))', re.M | re.S)
with open(keybindingfile) as f:
   content = f.read()
keybindings = json.loads(RE_COMMENTS.sub('', content))
bizoo
 
Posts: 886
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests