Sublime Forum

Do event listeners only listen to "built-in" events?

#1

Do event listeners only listen to “built-in” events?

I tried setting up an EventListener to listen for the execution of one of my TextCommands. Unfortunately, my simple test (below) seems to not be working.

When the EventListener runs, it prints out the command_name. Right now, it prints out the names for “built-in” text commands such as “copy”, “paste”, etc.

When I try to run my custom text command, via the console
v = sublime.active_window().active_view()
v.run_command(“test”)

The EventListener does not print out the text command_name: “test” as I would expect.

Am I missing something? Or do EventListeners only listen to “built-in” commands?

Thanks for any help :slight_smile:

import sublime, sublime_plugin

class TestCommand(sublime_plugin.TextCommand):

def run(self, edit):
	print("running TestCommand")

class TestListener(sublime_plugin.EventListener):

def on_post_text_command(self, view, command_name, args):
	# this text command just ran
	print('post command_name', command_name)
1 Like

#2

The on_post_text_command listener does not seem to listen to commands called via view.run_command (?), but should also print it if you trigger it via a keybinding.

Btw. You can also run commands in the console via view.run_command("test"), because view and window are defined as expected.

0 Likes

#3

That seems to be the case. I have no idea why.
I set up a key binding to trigger the command and it behaves as one would expect.

I also discovered that if one has a TextCommand that runs another TextCommand, the EventListener will only “hear” the triggering TextCommand, not the one executed from within…

So it would seem the EventListener on_post_text_command is only listening to TextCommands triggered via key-binding or menu interaction and NOT listening to TextCommands triggered via code or console.

0 Likes