Home Download Buy Blog Forum Support

Propagating captured keypress from key bindings

Propagating captured keypress from key bindings

Postby thanpolas on Fri Nov 16, 2012 7:19 pm

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?
thanpolas
 
Posts: 3
Joined: Fri Nov 16, 2012 7:12 pm

Re: Propagating captured keypress from key bindings

Postby C0D312 on Sat Nov 17, 2012 12:07 am

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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Propagating captured keypress from key bindings

Postby facelessuser on Sat Nov 17, 2012 12:11 am

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

Re: Propagating captured keypress from key bindings

Postby bizoo on Sat Nov 17, 2012 1:00 am

facelessuser wrote: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?

Exactly what I've in my mind, but never try it.
Is the on_query_context only called when the keymap is pressed ?
bizoo
 
Posts: 888
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Propagating captured keypress from key bindings

Postby C0D312 on Sat Nov 17, 2012 1:32 am

Hmm... ignore my post and try Facelessuser's suggestion. Sounds like it should work.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Propagating captured keypress from key bindings

Postby facelessuser on Sat Nov 17, 2012 3:51 am

bizoo wrote:
facelessuser wrote: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?

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


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

Re: Propagating captured keypress from key bindings

Postby thanpolas on Sat Nov 17, 2012 11:15 pm

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:
Code: Select all
  {
    "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?
thanpolas
 
Posts: 3
Joined: Fri Nov 16, 2012 7:12 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests