简单绑定到小部件按键事件

在按键上调用事件处理程序的最简单方法是将处理程序连接到 key-press-event 信号。在此示例中,我们为整个窗口注册事件,但你也可以注册单个窗口小部件。

最重要的部分是处理程序与事件的连接:

self.connect("key-press-event",self.on_key_press_event)

在事件处理程序中,窗口小部件和按键事件作为参数传入。键按键修改器如 Ctrl 键在 event.state 中可用,按下的键是 event.keyval

修饰键的值可在 Gdk.ModiferType 中找到,包括 CONTROL_MASKSHIFT_MASK 和其他几个。

关键值在 Gdk 中找到,前缀为 KEY_ ,例如,h 关键是 Gdk.KEY_h)这些可以使用 Gdk.keyval_name() 转换为字符串。

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

class MyWindow(Gtk.Window):

    key = Gdk.KEY_h

    def __init__(self):
        # init the base class (Gtk.Window)
        super().__init__()

        # state affected by shortcuts
        self.shortcut_hits = 0

        # Tell Gtk what to do when the window is closed (in this case quit the main loop)
        self.connect("delete-event", Gtk.main_quit)

        # connect the key-press event - this will call the keypress
        # handler when any key is pressed
        self.connect("key-press-event",self.on_key_press_event)

        # Window content goes in a vertical box
        box = Gtk.VBox()

        # mapping between Gdk.KEY_h and a string
        keyname = Gdk.keyval_name(self.key)

        # a helpful label
        instruct = Gtk.Label(label="Press Ctrl+%s" % keyname)
        box.add(instruct)

        # the label that will respond to the event
        self.label = Gtk.Label(label="")
        self.update_label_text()

        # Add the label to the window
        box.add(self.label)

        self.add(box)

    def on_key_press_event(self, widget, event):

        print("Key press on widget: ", widget)
        print("          Modifiers: ", event.state)
        print("      Key val, name: ", event.keyval, Gdk.keyval_name(event.keyval))

        # check the event modifiers (can also use SHIFTMASK, etc)
        ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)

        # see if we recognise a keypress
        if ctrl and event.keyval == Gdk.KEY_h:
            self.shortcut_hits += 1
            self.update_label_text()

    def update_label_text(self):
        # Update the label based on the state of the hit variable
        self.label.set_text("Shortcut pressed %d times" % self.shortcut_hits)

if __name__ == "__main__":
    win = MyWindow()
    win.show_all()

    # Start the Gtk main loop
    Gtk.main()

使用加速器组( Gtk.AccelGroup ) 可以实现应用程序范围快捷方式的更高级行为,但通常只需快速按键处理程序即可捕获特定小组件所需的键盘事件。