Sublime Forum

Retrieve Key Bindings

#1

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”?


	{ "keys": "super+f8"], "command": "test" },
	{ "keys": "super+shift+c"], "command": "test2" }
]

Thank you,

r.

0 Likes

#2

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.

0 Likes

#3

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

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.

0 Likes

#4

I figure it should be possible:

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 :question:

0 Likes

#5

Thank you,

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

:smile:

r.

0 Likes

#6

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? :smile:

r.

0 Likes

#7

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

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?

0 Likes

#8

[quote=“svenax”]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

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?[/quote]

No that’s exactly what I’d like :smile: Unfortunately:

TypeError: 'Settings' object is not iterable
0 Likes

#9

[quote=“ostinelli”]No that’s exactly what I’d like :smile: Unfortunately:

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:

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.

0 Likes

#10

I use this code:

RE_COMMENTS = re.compile('((//.*?$)|(/\*.*\*/))', re.M | re.S) with open(keybindingfile) as f: content = f.read() keybindings = json.loads(RE_COMMENTS.sub('', content))

0 Likes

#11

[quote=“bizoo”]

I use this code:

RE_COMMENTS = re.compile('((//.*?$)|(/\*.*\*/))', re.M | re.S) with open(keybindingfile) as f: content = f.read() keybindings = json.loads(RE_COMMENTS.sub('', content))[/quote]

Ah yes, that should work. As long as you dont have text that looks like comments inside strings in the json file that is …

0 Likes

#12

You’re right, it’s not fool proof.

This is probably a better solution (didn’t try it):
https://github.com/getify/JSON.minify/blob/master/minify_json.py

0 Likes

#13

thank you!

will use this as a starting point and will post back if i find better solutions!

r.

0 Likes