Sublime Forum

Key Down Event

#1

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
0 Likes

#2

There isn’t. The thing currently would be on_selection_modified.

0 Likes

#3

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.

github.com/AdamPflug/SublimeCenterCurrentLine

0 Likes

#4

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: https://forum.sublimetext.com/t/always-centered-cursor/4005/1 Might help. Good Luck!

0 Likes

Help trimming full path in status bar [SOLVED]
#5

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]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()

[/code]

0 Likes