我使用的是gtk-rs,并希望能够检测到何时按下任何键。
从一些在线搜索来看,在C中实现这一目标的方法似乎是使用gtk_widget_add_events,然后使用g_signal_connect。This answer有一个很好的解释。
在罗斯特,我可以给Widget::add_events打电话。我还找到了g_signal_connect_*的几个定义。但是,这些函数是unsafe,没有文档,而且似乎采用C类型作为参数。
我的问题是:
gobject_sys::g_signal_connect_closure,如何创建GObject和GClosure。在铁锈?铁锈结构和闭包可以转换成这样吗?发布于 2021-03-09 18:52:08
我想通了。
Thansk @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();https://stackoverflow.com/questions/66541725
复制相似问题