GTK +:为VTE添加自定义加速器

时间:2016-04-19 16:40:16

标签: python gtk

我在向Gtk.EventBox()上显示的菜单中添加加速器时遇到问题,该菜单只包含一个Vte.Terminal()。菜单显示正常,复制和粘贴工作正常,但加速器似乎没有工作。在他们到达我的eventBox之前它们被VTE抓住(奇怪的是,'因为我的事件框在vte小部件之上),例如,Ctrl + Shift + C在终端上作为Ctrl + C工作,只是中断当前进程。关于如何解决这个问题的任何想法?

menuitem-accelerator的相关关联是注释代码。

def terminalBox(self, terminal):
    """Given a terminal, creates an EventBox for the Box that has as
    a children said terminal"""
    eventTerminalBox = Gtk.EventBox()
    terminalBox = Gtk.Box()
    terminalBox.pack_start(terminal, True, True, 0)
    eventTerminalBox.connect("button_press_event", self.right_click)
    eventTerminalBox.add(terminalBox)
    return eventTerminalBox

def right_click(self, eventbox, event):
    """Defines the menu created when a user rightclicks on the
    terminal eventbox"""
    menu = Gtk.Menu()
    copy = Gtk.MenuItem("Copy")
    paste = Gtk.MenuItem("Paste")
    menu.append(paste)
    menu.append(copy)

    # TODO: make accelerators for copy paste work. add accel for paste
    #accelgroup = Gtk.AccelGroup()
    #self.add_accel_group(accelgroup)
    #accellabel = Gtk.AccelLabel("Copy/Paste")
    #accellabel.set_hexpand(True)
    #copy.add_accelerator("activate",
    #                     accelgroup,
    #                     Gdk.keyval_from_name("c"),
    #                     Gdk.ModifierType.SHIFT_MASK |
    #                     Gdk.ModifierType.CONTROL_MASK,
    #                     Gtk.AccelFlags.VISIBLE)

    copy.connect("activate", self.copy_text)
    paste.connect("activate", self.paste_text)

    copy.show()
    paste.show()
    menu.popup(None, None, None, None, event.button, event.time)

def copy_text(self, button):
    """What happens when the user copies text"""
    content = self.selection_clipboard.wait_for_text()
    self.clipboard.set_text(content, -1)

def paste_text(self, button):
    """What happens when the user pastes text"""
    currentTerminal = self.getCurrentFocusedTerminal()
    currentTerminal.paste_clipboard()

1 个答案:

答案 0 :(得分:0)

好的,万一其他人需要帮助。我放弃了GTK菜单加速器,所以转向我的Vte Widget。在那里,我将key_press_event信号连接到一个方法,该方法检测按下了哪些键,如果它找到Ctrl + Shift的组合,则返回。返回很重要,因为这是阻止VTE实际使用shorcut执行其他操作的原因,例如在Ctrl + Shift + C上终止进程。

class Terminal(Vte.Terminal):
    """Defines a simple terminal"""
    def __init__(self, CONF):
        super(Vte.Terminal, self).__init__()

        self.pty = self.pty_new_sync(Vte.PtyFlags.DEFAULT, None)
        self.set_pty(self.pty)
        self.connect("key_press_event", self.copy_or_paste)

        self.set_scrollback_lines(-1)
        self.set_audible_bell(0)

    def copy_or_paste(self, widget, event):
        """Decides if the Ctrl+Shift is pressed, in which case returns True.
        If Ctrl+Shift+C or Ctrl+Shift+V are pressed, copies or pastes,
        acordingly. Return necesary so it doesn't perform other action,
        like killing the process, on Ctrl+C.
        """

        control_key = Gdk.ModifierType.CONTROL_MASK
        shift_key = Gdk.ModifierType.SHIFT_MASK
        if event.type == Gdk.EventType.KEY_PRESS:
            if event.state == shift_key | control_key: #both shift  and control
                if event.keyval == 67: # that's the C key
                    self.copy_clipboard()
                elif event.keyval == 86: # and that's the V key
                    self.paste_clipboard()
                return True