Home Download Buy Blog Forum Support

Key Down Event

Key Down Event

Postby AdamPflug on Mon Jan 30, 2012 6:11 am

Hey Guys,

I'm working on a plugin that keeps the currently selected line centered in the screen, but I'm having a hard time finding an event I can use to trigger this re-centering. Is there some kind of on_key_down type event I can tie into?

- Adam
AdamPflug
 
Posts: 2
Joined: Mon Jan 30, 2012 6:07 am

Re: Key Down Event

Postby jps on Mon Jan 30, 2012 6:59 am

There isn't. The thing currently would be on_selection_modified.
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: Key Down Event

Postby AdamPflug on Mon Jan 30, 2012 6:40 pm

Cool, thanks a lot! That works, except it behaves really odd when you try to drag select. Is there a way to check if the mouse is down (or some other way to know that the selection isn't "finalized"). Otherwise I'm going to have to look into rate-limiting the centering or something (works great right now for keyboard or just clicking around.)

Here's the code if that helps. Pretty basic.

https://github.com/AdamPflug/SublimeCenterCurrentLine
AdamPflug
 
Posts: 2
Joined: Mon Jan 30, 2012 6:07 am

Re: Key Down Event

Postby C0D312 on Mon Jan 30, 2012 8:59 pm

I was going to recommend you look into sublime mouse maps, but that might be overkill for your simple plugin. What your describing sounds a lot like this: http://www.sublimetext.com/forum/viewtopic.php?f=6&t=4806 Might help. Good Luck!
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Key Down Event

Postby facelessuser on Mon Jan 30, 2012 10:55 pm

You could do something like this to initiate a small wait on the on_selection_modified event and issue an id that gets changed on every event. The id must match for the main code to get launched. So as you are drag selecting the id keeps changing so the main code does not get executed until you take a long enough pause. It should alleviate a lot of what you are experiencing.

There will be some slight issue if you select some code and quickly try and scroll, the code will kick in milliseconds later scrolling to center on your selection. If you change the wait to 100ms it might avoid this issue. You can play with it and find the sweet spot.

Code: Select all
import sublime_plugin
import sublime
from random import randrange

DEFAULT_WAIT = 300


class CenterCurrentLineCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
        settings = sublime.load_settings('Default.sublime-settings')
        view.settings().set('center_current_line', settings.get('center_current_line',  True))

    def run(self, args):
        sel = self.view.sel()
        if len(sel) == 1 and self.view.settings().get('center_current_line'):
            self.view.show_at_center(sel[0])


class CenterCurrentLineOnCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view

    def run(self, args):
        self.view.settings().set('center_current_line', True)


class CenterCurrentLineOffCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view

    def run(self, args):
        self.view.settings().set('center_current_line', False)


class CenterLineEventHandler(sublime_plugin.EventListener):
    def __init__(self):
        self.wait_id = None

    def check_wait(self, wait_id):
        if self.wait_id != wait_id:
            new_wait_id = randrange(1, 999999)
            self.wait_id = new_wait_id
            sublime.set_timeout(
                lambda: self.check_wait(wait_id=new_wait_id),
                self.wait_ms
            )
        else:
            self.wait_id = None
            window = sublime.active_window()
            view = window.active_view() if window != None else None
            if view != None:
                view.run_command('center_current_line')

    def wait(self):
        new_wait_id = randrange(1, 999999)
        if self.wait_id == None:
            self.wait_id = new_wait_id
            sublime.set_timeout(
                lambda: self.check_wait(wait_id=new_wait_id),
                self.wait_ms
            )
        else:
            self.wait_id = new_wait_id

    def on_selection_modified(self, view):
        self.wait_ms = view.settings().get('center_current_line_wait', DEFAULT_WAIT)
        self.wait()
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests