I found Rust GTK documentation pretty weird. Maybe I’m wrong. I hope so. But it looks like that.
Ok. Here is the piece of code. It can listen to keyboard events and print keys combinations it terminal: “Key name” and “Modifier” (for example Shift key). And values[1] is our keyboard event
window .connect("key_press_event", false, |values| { let raw_event = &values[1].get::<gdk::Event>().unwrap(); match raw_event.downcast_ref::<gdk::EventKey>() { Some(event) => { println!("Key name: {:?}", event.keyval()); println!("Modifier: {:?}", event.state()); }, None => {}, } let result = glib::value::Value::from_type(glib::types::Type::BOOL); Some(result) });
I have to figure out how it works. And then put it into practice, for example add this functionality to my GTK calculator.
Cargo.toml
[dependencies] glib = "0.15.12" gdk = "0.15.4" gtk = "0.15.5"
main.rs
use gtk::prelude::*; mod gui; fn main() { let application = gtk::Application::new( Some("com.github.gtk-rs.gtk_keyboard_events_listener"), Default::default(), ); application.connect_activate(gui::build_ui); application.run(); }
Permanent link to the gui.rs file
Top comments (0)