GTK+:向 VTE 添加自定义加速器
GTK+: Add custom accelerator to VTE
我在向出现在仅包含 Vte.Terminal() 的 Gtk.EventBox() 上的菜单中添加快捷键时遇到问题。菜单显示正常,复制和粘贴工作正常,但加速器似乎不起作用。它们在到达我的 eventBox 之前就被 VTE 抓住了(奇怪的是,因为我的 eventbox 在 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()
好的,以防其他人需要这方面的帮助。我放弃了 GTK 菜单加速器,所以转而使用我的 Vte Widget。在那里,我将 key_press_event 信号连接到一种方法,该方法检测按下了哪些键,如果它找到 Ctrl+Shift 的组合,则它是 returns。 return 很重要,因为这是阻止 VTE 使用快捷方式实际执行其他操作的原因,例如在 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
我在向出现在仅包含 Vte.Terminal() 的 Gtk.EventBox() 上的菜单中添加快捷键时遇到问题。菜单显示正常,复制和粘贴工作正常,但加速器似乎不起作用。它们在到达我的 eventBox 之前就被 VTE 抓住了(奇怪的是,因为我的 eventbox 在 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()
好的,以防其他人需要这方面的帮助。我放弃了 GTK 菜单加速器,所以转而使用我的 Vte Widget。在那里,我将 key_press_event 信号连接到一种方法,该方法检测按下了哪些键,如果它找到 Ctrl+Shift 的组合,则它是 returns。 return 很重要,因为这是阻止 VTE 使用快捷方式实际执行其他操作的原因,例如在 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