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