Hi! Thanks for your reply! I am currently using the library directly from the ubuntu package manager installed by sudo apt-get install libgtk-4-dev
.
The
Input Method
is always gtk-im-context-simple
in my gtk4 testing app(on gtk 4.6.6 with x11 backend). Meanwhile, for my gtk3 testing app, the inspector does not show the info, but I assume that it should be the same.
This is the gtk4 code for the testing app
#include <gtk/gtk.h>
static void on_key_press(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data) {
g_print("Key Press: %u\n", keyval);
}
static void on_key_release(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data) {
g_print("Key Release: %u\n", keyval);
}
static void on_commit(GtkEditable *editable, const gchar *text, gpointer data) {
g_print("Commit: %s\n", text);
}
static void on_preedit_changed(GtkIMContext *context, gpointer data) {
gchar *preedit_string = NULL;
gtk_im_context_get_preedit_string(context, &preedit_string, NULL, NULL);
g_print("Preedit Changed: %s\n", preedit_string);
g_free(preedit_string);
}
static void on_activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "GTK4 Entry Example");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
GtkWidget *entry = gtk_entry_new();
gtk_window_set_child(GTK_WINDOW(window), entry);
// GtkEventController *controller = gtk_event_controller_key_new();
// g_signal_connect(controller, "key-pressed", G_CALLBACK(on_key_press), NULL);
// g_signal_connect(controller, "key-released", G_CALLBACK(on_key_release), NULL);
// gtk_widget_add_controller(entry, controller);
// GtkIMContext *im_context = gtk_im_multicontext_new();
// gtk_event_controller_key_set_im_context ((GtkEventControllerKey *)controller, im_context);
// g_signal_connect(im_context, "commit", G_CALLBACK(on_commit), NULL);
// g_signal_connect(im_context, "preedit-changed", G_CALLBACK(on_preedit_changed), NULL);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char *argv[]) {
GtkApplication *app = gtk_application_new("com.example.gtk4_entry_example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
Note: no matter whether the commented lines are added or not, the IME for Chinese is always not working. BUT Chinese input methods work correctly in my show applications search bar, terminal, browser, etc.