Sublime Forum

Propagating captured keypress from key bindings

#1

Hello all,

I was wondering how i could propagate the keypress that triggered the execution of my plugin.

I want to bind some casual keys (like enter, up / down arrow) and run my plugin when each of them is pressed.

However the problem is that once i bind for these keys, pressing them does not get propagated to the application, the keypresses die with the execution of my plugin.

This may be expected behavior, but how can i let the keypress perform its default action? (e.g. change a line when enter is pressed).

Currently, i am emulating the keypress behavior but i know this must not be the right thing to do, for example in the case of an enter, while i insert a newline, the auto_indent doesn’t kick in so the cursor moves at column 0.

How can i “release” the keypress event and let it bubble / propagate?

0 Likes

Keystroke detection
#2

I too was once looking for a way to bind a command to a key without swallowing the command… It’s on my wishlist. You could try fiddling with on_modified and checking the previous char (that won’t be helpful for enter or the arrow keys unfortunately), but I don’t think that will get you anywhere. Sorry.

0 Likes

#3

Couldn’t you technically do it in a on_query_context have on query context actually do or call a function to do what you want, and then return False?

0 Likes

#4

Exactly what I’ve in my mind, but never try it.
Is the on_query_context only called when the keymap is pressed ?

0 Likes

#5

Hmm… ignore my post and try Facelessuser’s suggestion. Sounds like it should work.

0 Likes

#6

[quote=“bizoo”]

Exactly what I’ve in my mind, but never try it.
Is the on_query_context only called when the keymap is pressed ?[/quote]

Yes, only when keymap is pressed, it is like a key event. I do code in on_query_context in some plugins, I usually do it in preparation for the command that is going to run when I have on_query_context accept it (maybe set some flags or whatever). But there is nothing saying you can’t execute the payload there, and then if you return False, ST2 will assume that keymap hasn’t been accepted and just keep looking for a command to accept the shortcut…essentially allowing the the keymap to trigger one command and bubble up (down?) to the next keymap command.

0 Likes

#7

Thank you facelessuser , on_query_context() event seems to do the job.

My only issue now is that for the up and down key bindings, on_query_context gets triggered twice…

That’s my binding:

{ "keys": "down"], "command": "auto_doc_blockr_void", "context": { "key": "setting.auto_indent", "operator": "equal", "operand": true, "match_all": true }, { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, {"key" : "autodoc.down"} ] }

This triggers the event twice, even if i return False on the event func…

Any ideas?

0 Likes