我想到了。
谢谢@Jmb,Widget::connect
是正确的方法。但是,该函数没有记录,并且有一些非常奇怪的类型。这是我弄清楚如何使用它的方法:
window
.connect("key_press_event", false, |values| {
// "values" is a 2-long slice of glib::value::Value, which wrap G-types
// You can unwrap them if you know what type they are going to be ahead of time
// values[0] is the window and values[1] is the event
let raw_event = &values[1].get::<gdk::Event>().unwrap().unwrap();
// You have to cast to the correct event type to access some of the fields
match raw_event.downcast_ref::<gdk::EventKey>() {
Some(event) => {
println!("key value: {:?}", std::char::from_u32(event.get_keyval()));
println!("modifiers: {:?}", event.get_state());
},
None => {},
}
// You need to return Some() of a glib Value wrapping a bool
let result = glib::value::Value::from_type(glib::types::Type::Bool);
// I can't figure out how to actually set the value of result
// Luckally returning false is good enough for now.
Some(result)
})
.unwrap();