Ich habe Folgendes verwendet Dokumentation eine Methode in Vala zu schreiben, um Tastendruckereignisse (z.B. <Strg>V) an die Anwendung zu senden, die den Fokus hat.
Hier ist der Code:
public void press(string accelerator)
{
X.KeyEvent key_event;
if(create_key_event(accelerator, X.EventType.KeyPress, out key_event)) {
X.Event event = (X.Event)key_event;
key_event.display.send_event(key_event.window, false,
X.EventMask.KeyPressMask, ref event);
}
}
private bool create_key_event(string accelerator,
int event_type, out X.KeyEvent key_event)
{
// convert accelerator
uint keysym;
Gdk.ModifierType modifiers;
Gtk.accelerator_parse(accelerator, out keysym, out modifiers);
unowned X.Display display = Gdk.x11_get_default_xdisplay();
key_event = X.KeyEvent();
int keycode = display.keysym_to_keycode(keysym);
if(keycode != 0) {
X.Window root_window = Gdk.x11_get_default_root_xwindow();
// get window with focus
X.Window focus;
int revert_to_return;
display.get_input_focus(out focus, out revert_to_return);
key_event.display = display;
key_event.root = root_window;
key_event.window = focus;
key_event.subwindow = X.None;
key_event.time = X.CURRENT_TIME;
key_event.keycode = keycode;
key_event.state = modifiers;
key_event.type = event_type;
key_event.x = 1;
key_event.y = 1;
key_event.x_root = 1;
key_event.y_root = 1;
return true;
}
return false;
}
Dies funktioniert nur bei gtk2-Anwendungen. Allerdings scheinen gtk3-Anwendungen solche Ereignisse gänzlich zu ignorieren. Gibt es eine Möglichkeit, solche Ereignisse auch an gtk3-Anwendungen zu senden?