2016-04-19 47 views
1

將加速器添加到僅包含Vte.Terminal()的Gtk.EventBox()上出現的菜單中時出現問題。菜單顯示OK,複製和粘貼工作正常,但加速器似乎不起作用。他們被VTE抓到之前,他們到達我的eventBox(奇怪,因爲我的事件箱高於vte小部件),例如,Ctrl + Shift + C作爲Ctrl + C在終端上,只是中斷當前的過程。有關如何去做這件事的任何想法?GTK +:向VTE添加自定義加速器

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

回答

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