Text tag events

Hi, I’m trying to create a text tag that changes the key press behavior. According to the Text widget conceptual overview, this is possible:

They [tags] can instead affect the behavior of mouse and key presses, “lock” a range of text so the user can’t edit it, or countless other things.

I think I should connect to the event signal of the tag. This works for button presses, but not for key presses. What am I doing wrong? Example code:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk


def link_event_cb(tag, object_, event, iter_):
    if event.get_event_type() == Gdk.EventType.BUTTON_PRESS:
        print("Link pressed") # This does work 
    if event.get_event_type() == Gdk.EventType.KEY_RELEASE:
        print("Button pressed while cursor in link") # This does not work
    return False
        

win = Gtk.Window()
win.set_default_size(300,50)
win.connect("destroy", Gtk.main_quit)

# Create textview and add a text to it
view = Gtk.TextView()
buf = view.get_buffer()
buf.set_text("This is my long text with a link in it.")
win.add(view)

    
# Create a tag and apply it
link = buf.create_tag( None, foreground = "#FF0000")
start_it = buf.get_iter_at_offset(28)
end_it = buf.get_iter_at_offset(32)
buf.apply_tag(link,start_it,end_it)

# Connect the event signal to the tag
link.connect('event',link_event_cb)


win.show_all()
Gtk.main()

Thanks for the help!
Andras

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.